Skip to content

Commit c19b6d5

Browse files
committed
fix: react-hot-loader compatibility
1 parent 94799ef commit c19b6d5

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

src/components/connectAdvanced.js

+41-11
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ export default function connectAdvanced(
120120
const { pure } = connectOptions
121121

122122
let OuterBaseComponent = Component
123-
let FinalWrappedComponent = WrappedComponent
124123

125124
if (pure) {
126125
OuterBaseComponent = PureComponent
@@ -131,15 +130,25 @@ export default function connectAdvanced(
131130
let lastState
132131
let lastDerivedProps
133132
let lastStore
133+
let lastSelectorFactoryOptions
134134
let sourceSelector
135135

136-
return function selectDerivedProps(state, props, store) {
136+
return function selectDerivedProps(
137+
state,
138+
props,
139+
store,
140+
selectorFactoryOptions
141+
) {
137142
if (pure && lastProps === props && lastState === state) {
138143
return lastDerivedProps
139144
}
140145

141-
if (store !== lastStore) {
146+
if (
147+
store !== lastStore ||
148+
lastSelectorFactoryOptions !== selectorFactoryOptions
149+
) {
142150
lastStore = store
151+
lastSelectorFactoryOptions = selectorFactoryOptions
143152
sourceSelector = selectorFactory(
144153
store.dispatch,
145154
selectorFactoryOptions
@@ -157,14 +166,23 @@ export default function connectAdvanced(
157166
}
158167

159168
function makeChildElementSelector() {
160-
let lastChildProps, lastForwardRef, lastChildElement
169+
let lastChildProps, lastForwardRef, lastChildElement, lastComponent
161170

162-
return function selectChildElement(childProps, forwardRef) {
163-
if (childProps !== lastChildProps || forwardRef !== lastForwardRef) {
171+
return function selectChildElement(
172+
WrappedComponent,
173+
childProps,
174+
forwardRef
175+
) {
176+
if (
177+
childProps !== lastChildProps ||
178+
forwardRef !== lastForwardRef ||
179+
lastComponent !== WrappedComponent
180+
) {
164181
lastChildProps = childProps
165182
lastForwardRef = forwardRef
183+
lastComponent = WrappedComponent
166184
lastChildElement = (
167-
<FinalWrappedComponent {...childProps} ref={forwardRef} />
185+
<WrappedComponent {...childProps} ref={forwardRef} />
168186
)
169187
}
170188

@@ -182,7 +200,14 @@ export default function connectAdvanced(
182200
)
183201
this.selectDerivedProps = makeDerivedPropsSelector()
184202
this.selectChildElement = makeChildElementSelector()
185-
this.renderWrappedComponent = this.renderWrappedComponent.bind(this)
203+
this.indirectRenderWrappedComponent = this.indirectRenderWrappedComponent.bind(
204+
this
205+
)
206+
}
207+
208+
indirectRenderWrappedComponent(value) {
209+
// calling renderWrappedComponent on prototype from indirectRenderWrappedComponent bound to `this`
210+
return this.renderWrappedComponent(value)
186211
}
187212

188213
renderWrappedComponent(value) {
@@ -206,18 +231,23 @@ export default function connectAdvanced(
206231
let derivedProps = this.selectDerivedProps(
207232
storeState,
208233
wrapperProps,
209-
store
234+
store,
235+
selectorFactoryOptions
210236
)
211237

212-
return this.selectChildElement(derivedProps, forwardedRef)
238+
return this.selectChildElement(
239+
WrappedComponent,
240+
derivedProps,
241+
forwardedRef
242+
)
213243
}
214244

215245
render() {
216246
const ContextToUse = this.props.context || Context
217247

218248
return (
219249
<ContextToUse.Consumer>
220-
{this.renderWrappedComponent}
250+
{this.indirectRenderWrappedComponent}
221251
</ContextToUse.Consumer>
222252
)
223253
}

0 commit comments

Comments
 (0)