Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: react-hot-loader compatibility #1137

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 41 additions & 11 deletions src/components/connectAdvanced.js
Expand Up @@ -120,7 +120,6 @@ export default function connectAdvanced(
const { pure } = connectOptions

let OuterBaseComponent = Component
let FinalWrappedComponent = WrappedComponent

if (pure) {
OuterBaseComponent = PureComponent
Expand All @@ -131,15 +130,25 @@ export default function connectAdvanced(
let lastState
let lastDerivedProps
let lastStore
let lastSelectorFactoryOptions
let sourceSelector

return function selectDerivedProps(state, props, store) {
return function selectDerivedProps(
state,
props,
store,
selectorFactoryOptions
) {
if (pure && lastProps === props && lastState === state) {
return lastDerivedProps
}

if (store !== lastStore) {
if (
store !== lastStore ||
lastSelectorFactoryOptions !== selectorFactoryOptions
Copy link
Author

@theKashey theKashey Dec 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

factory could change, not only store

) {
lastStore = store
lastSelectorFactoryOptions = selectorFactoryOptions
sourceSelector = selectorFactory(
store.dispatch,
selectorFactoryOptions
Expand All @@ -157,14 +166,23 @@ export default function connectAdvanced(
}

function makeChildElementSelector() {
let lastChildProps, lastForwardRef, lastChildElement
let lastChildProps, lastForwardRef, lastChildElement, lastComponent

return function selectChildElement(childProps, forwardRef) {
if (childProps !== lastChildProps || forwardRef !== lastForwardRef) {
return function selectChildElement(
WrappedComponent,
childProps,
forwardRef
) {
if (
childProps !== lastChildProps ||
forwardRef !== lastForwardRef ||
lastComponent !== WrappedComponent
) {
lastChildProps = childProps
lastForwardRef = forwardRef
lastComponent = WrappedComponent
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

component could change. Don't use component from a local scope.

lastChildElement = (
<FinalWrappedComponent {...childProps} ref={forwardRef} />
<WrappedComponent {...childProps} ref={forwardRef} />
)
}

Expand All @@ -182,7 +200,14 @@ export default function connectAdvanced(
)
this.selectDerivedProps = makeDerivedPropsSelector()
this.selectChildElement = makeChildElementSelector()
this.renderWrappedComponent = this.renderWrappedComponent.bind(this)
this.indirectRenderWrappedComponent = this.indirectRenderWrappedComponent.bind(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constructor could not be updated on hot-load.

this
)
}

indirectRenderWrappedComponent(value) {
// calling renderWrappedComponent on prototype from indirectRenderWrappedComponent bound to `this`
return this.renderWrappedComponent(value)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the main trick - redirect to hot-reloadable prototype level.

}

renderWrappedComponent(value) {
Expand All @@ -206,18 +231,23 @@ export default function connectAdvanced(
let derivedProps = this.selectDerivedProps(
storeState,
wrapperProps,
store
store,
selectorFactoryOptions
)

return this.selectChildElement(derivedProps, forwardedRef)
return this.selectChildElement(
WrappedComponent,
derivedProps,
forwardedRef
)
}

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

return (
<ContextToUse.Consumer>
{this.renderWrappedComponent}
{this.indirectRenderWrappedComponent}
</ContextToUse.Consumer>
)
}
Expand Down