Skip to content

Commit

Permalink
memoize renderedWrappedComponent separately to prevent this step from…
Browse files Browse the repository at this point in the history
… being performed when redundant (reduxjs#1234)
  • Loading branch information
vzaidman authored and timdorr committed Apr 12, 2019
1 parent da4e446 commit ae98219
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/components/connectAdvanced.js
Expand Up @@ -375,14 +375,14 @@ export default function connectAdvanced(

// Now that all that's done, we can finally try to actually render the child component.
// We memoize the elements for the rendered child component as an optimization.
const renderedWrappedComponent = useMemo(
() => <WrappedComponent {...actualChildProps} ref={forwardedRef} />,
[forwardedRef, WrappedComponent, actualChildProps]
)

// If React sees the exact same element reference as last time, it bails out of re-rendering
// that child, same as if it was wrapped in React.memo() or returned false from shouldComponentUpdate.
const renderedChild = useMemo(() => {
// Render the actual child component
const renderedWrappedComponent = (
<WrappedComponent {...actualChildProps} ref={forwardedRef} />
)

if (shouldHandleStateChanges) {
// If this component is subscribed to store updates, we need to pass its own
// subscription instance down to our descendants. That means rendering the same
Expand All @@ -395,13 +395,7 @@ export default function connectAdvanced(
}

return renderedWrappedComponent
}, [
ContextToUse,
WrappedComponent,
actualChildProps,
forwardedRef,
overriddenContextValue
])
}, [ContextToUse, renderedWrappedComponent, overriddenContextValue])

return renderedChild
}
Expand Down

0 comments on commit ae98219

Please sign in to comment.