|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | import PropTypes from 'prop-types'; |
10 | | -import React, {type ComponentType} from 'react'; |
| 10 | +import React, {type ComponentType, useContext} from 'react'; |
11 | 11 | import ReactRedux from 'react-redux'; |
12 | 12 | import type {Reducer} from 'redux'; |
13 | 13 | import {createRPCHandler, createRPCReactors} from 'fusion-rpc-redux'; |
@@ -57,50 +57,30 @@ export function withRPCRedux<Props: {}>( |
57 | 57 | return (Component: ComponentType<Props>) => { |
58 | 58 | const withRPCRedux = (oldProps, context) => { |
59 | 59 | 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} />; |
104 | 84 | }; |
105 | 85 | const displayName = Component.displayName || Component.name || 'Anonymous'; |
106 | 86 | withRPCRedux.displayName = 'WithRPCRedux' + '(' + displayName + ')'; |
|
0 commit comments