Skip to content
This repository was archived by the owner on May 17, 2019. It is now read-only.

Commit 2614300

Browse files
author
Choong Kim
committed
Add conditional logic and update deps to allow for plugin to be used with react redux 5 as well
1 parent 96a6bc1 commit 2614300

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
"prop-types": "^15.7.2"
2929
},
3030
"peerDependencies": {
31-
"fusion-core": "^1.10.6",
31+
"fusion-core": "^1.10.3",
3232
"fusion-react": "^2.0.0",
33-
"react": "^16.8.6",
34-
"react-redux": "^7.0.2"
33+
"react": "14.x - 16.x",
34+
"react-redux": ">= 5.x"
3535
},
3636
"devDependencies": {
3737
"@babel/preset-react": "^7.0.0",

src/hoc.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import PropTypes from 'prop-types';
1010
import React, {type ComponentType} from 'react';
11-
import {ReactReduxContext} from 'react-redux';
11+
import ReactRedux from 'react-redux';
1212
import type {Reducer} from 'redux';
1313
import {createRPCHandler, createRPCReactors} from 'fusion-rpc-redux';
1414

@@ -57,8 +57,28 @@ export function withRPCRedux<Props: {}>(
5757
return (Component: ComponentType<Props>) => {
5858
const withRPCRedux = (oldProps, context) => {
5959
const {rpc} = context;
60-
return (
61-
<ReactReduxContext.Consumer>
60+
return context.store && !ReactRedux.ReactReduxContext ? (
61+
(function() {
62+
if (mapStateToParams) {
63+
const mapState = mapStateToParams;
64+
mapStateToParams = (state, args) => mapState(state, args, oldProps);
65+
}
66+
const handler = createRPCHandler({
67+
rpcId,
68+
rpc,
69+
store: context.store,
70+
actions,
71+
mapStateToParams,
72+
transformParams,
73+
});
74+
const props = {
75+
...oldProps,
76+
[propName]: handler,
77+
};
78+
return React.createElement(Component, props);
79+
})()
80+
) : (
81+
<ReactRedux.ReactReduxContext.Consumer>
6282
{({store}) => {
6383
if (mapStateToParams) {
6484
const mapState = mapStateToParams;
@@ -79,13 +99,14 @@ export function withRPCRedux<Props: {}>(
7999
};
80100
return React.createElement(Component, props);
81101
}}
82-
</ReactReduxContext.Consumer>
102+
</ReactRedux.ReactReduxContext.Consumer>
83103
);
84104
};
85105
const displayName = Component.displayName || Component.name || 'Anonymous';
86106
withRPCRedux.displayName = 'WithRPCRedux' + '(' + displayName + ')';
87107
withRPCRedux.contextTypes = {
88108
rpc: PropTypes.object.isRequired,
109+
store: PropTypes.object,
89110
};
90111
return withRPCRedux;
91112
};

0 commit comments

Comments
 (0)