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

Commit 0d4ea02

Browse files
author
Choong Kim
committed
Refactor using useContext and jsx
1 parent 2614300 commit 0d4ea02

File tree

2 files changed

+38
-45
lines changed

2 files changed

+38
-45
lines changed

.prettierrc.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
printWidth: 80,
3+
tabWidth: 2,
4+
useTabs: false,
5+
semi: true,
6+
singleQuote: true,
7+
trailingComma: 'es5',
8+
bracketSpacing: false,
9+
jsxBracketSameLine: false,
10+
rangeStart: 0,
11+
rangeEnd: Infinity,
12+
parser: 'babylon'
13+
};

src/hoc.js

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import PropTypes from 'prop-types';
10-
import React, {type ComponentType} from 'react';
10+
import React, {type ComponentType, useContext} from 'react';
1111
import ReactRedux from 'react-redux';
1212
import type {Reducer} from 'redux';
1313
import {createRPCHandler, createRPCReactors} from 'fusion-rpc-redux';
@@ -57,50 +57,30 @@ export function withRPCRedux<Props: {}>(
5757
return (Component: ComponentType<Props>) => {
5858
const withRPCRedux = (oldProps, context) => {
5959
const {rpc} = context;
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>
82-
{({store}) => {
83-
if (mapStateToParams) {
84-
const mapState = mapStateToParams;
85-
mapStateToParams = (state, args) =>
86-
mapState(state, args, oldProps);
87-
}
88-
const handler = createRPCHandler({
89-
rpcId,
90-
rpc,
91-
store,
92-
actions,
93-
mapStateToParams,
94-
transformParams,
95-
});
96-
const props = {
97-
...oldProps,
98-
[propName]: handler,
99-
};
100-
return React.createElement(Component, props);
101-
}}
102-
</ReactRedux.ReactReduxContext.Consumer>
103-
);
60+
const store =
61+
context.store || useContext(ReactRedux.ReactReduxContext).store;
62+
if (__DEV__ && context.store)
63+
console.error(
64+
'React-Redux 5.x is deprecated. Please upgrade your version of React-Redux'
65+
);
66+
if (mapStateToParams) {
67+
const mapState = mapStateToParams;
68+
mapStateToParams = (state, args) => mapState(state, args, oldProps);
69+
}
70+
71+
const handler = createRPCHandler({
72+
rpcId,
73+
rpc,
74+
store,
75+
actions,
76+
mapStateToParams,
77+
transformParams,
78+
});
79+
const props = {
80+
...oldProps,
81+
[propName]: handler,
82+
};
83+
return <Component {...props} />;
10484
};
10585
const displayName = Component.displayName || Component.name || 'Anonymous';
10686
withRPCRedux.displayName = 'WithRPCRedux' + '(' + displayName + ')';

0 commit comments

Comments
 (0)