Skip to content

Commit 8719eda

Browse files
vzaidmantimdorr
authored andcommitted
memoize renderedWrappedComponent separately to prevent this step from being performed when redundant (#1234)
1 parent 7555eae commit 8719eda

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

src/components/connectAdvanced.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -375,14 +375,14 @@ export default function connectAdvanced(
375375

376376
// Now that all that's done, we can finally try to actually render the child component.
377377
// We memoize the elements for the rendered child component as an optimization.
378+
const renderedWrappedComponent = useMemo(
379+
() => <WrappedComponent {...actualChildProps} ref={forwardedRef} />,
380+
[forwardedRef, WrappedComponent, actualChildProps]
381+
)
382+
378383
// If React sees the exact same element reference as last time, it bails out of re-rendering
379384
// that child, same as if it was wrapped in React.memo() or returned false from shouldComponentUpdate.
380385
const renderedChild = useMemo(() => {
381-
// Render the actual child component
382-
const renderedWrappedComponent = (
383-
<WrappedComponent {...actualChildProps} ref={forwardedRef} />
384-
)
385-
386386
if (shouldHandleStateChanges) {
387387
// If this component is subscribed to store updates, we need to pass its own
388388
// subscription instance down to our descendants. That means rendering the same
@@ -395,13 +395,7 @@ export default function connectAdvanced(
395395
}
396396

397397
return renderedWrappedComponent
398-
}, [
399-
ContextToUse,
400-
WrappedComponent,
401-
actualChildProps,
402-
forwardedRef,
403-
overriddenContextValue
404-
])
398+
}, [ContextToUse, renderedWrappedComponent, overriddenContextValue])
405399

406400
return renderedChild
407401
}

0 commit comments

Comments
 (0)