Skip to content

Commit 37e2ce7

Browse files
theKasheymarkerikson
authored andcommitted
fix: react-hot-loader compatibility
1 parent 63af52f commit 37e2ce7

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

src/components/connectAdvanced.js

Lines changed: 41 additions & 11 deletions
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,10 +231,15 @@ 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() {
@@ -222,7 +252,7 @@ export default function connectAdvanced(
222252

223253
return (
224254
<ContextToUse.Consumer>
225-
{this.renderWrappedComponent}
255+
{this.indirectRenderWrappedComponent}
226256
</ContextToUse.Consumer>
227257
)
228258
}

0 commit comments

Comments
 (0)