SlotFill: add dependencies to updateFill effect#77907
Conversation
|
Size Change: +2 B (0%) Total Size: 7.87 MB 📦 View Changed
ℹ️ View Unchanged
|
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Flaky tests detected in c7c3743. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/25310382847
|
Mamaduka
left a comment
There was a problem hiding this comment.
Looks good to me ✅
Adding deps technically won't change the number of times this effect is called.
| registry.updateFill( name, { | ||
| instance: instanceRef.current, | ||
| children: childrenRef.current, | ||
| children, |
There was a problem hiding this comment.
I wonder how this might impact the separate useLayoutEffect above. Do we need both of them? Why does the other use childrenRef and this one children ? (Do we need childrenRef at all?)
There was a problem hiding this comment.
These are three effects that collaborate together:
- we need to call
registerFillon mount andunregisterFillon unmount. TheregisterFillcall needs achildrenarg, but we don't want to re-register the fill on everychildrenchange. That's why the value needs to be passed into the effect as a ref. That's also the only reason whychildrenRefis needed. - we need to call
updateFillon every re-render, when newchildrenvalue is passed. This effect wants to run on everychildrenchange, so thechildrendependency is warranted. TheupdateFillmethod does its own internal check whetherprevChildren !== nextChildren, so even the effect without any dependencies, running on every render, was OK. It just raises eyebrows (both human and linter's).
Follow-up to #68056 which I promised in the review (#68056 (comment)) and now @obenland reminded me about it 🙂
Adds dependencies to layout effect that calls
updateFillwith updatedchildren.I don't think anything will really change after this PR.
childrenare typically different on every render (a new element created from JSX markup), so the effect will be triggered on every render. And it will store the latestchildrenand trigger a re-render of theSlot. Now we're just adding thechildrenexplicitly as dependency and avoid suspicious effect without dependencies.I'm also updating the
childrenRef.currentexpression to simplychildren. They are guaranteed to be the same, because an earllier layout effects callschildrenRef.current = children. And if I didn't do it, eslint would complain about a dependency that is not really used by the effect callback.