Skip to content

memoize renderedWrappedComponent separately to prevent this step from… #1234

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

Merged
merged 1 commit into from
Apr 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions src/components/connectAdvanced.js
Original file line number Diff line number Diff line change
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