Repository navigation
fix: don't remount slot content when Render re-renders - #1886
Open
gui-com-pt wants to merge 5 commits into
Open
gui-com-pt wants to merge 5 commits into
gui-com-pt wants to merge 5 commits into
Conversation
|
@gui-com-pt is attempting to deploy a commit to the Puck Team on Vercel. A member of the Team first needs to authorize it. |
There was a problem hiding this comment.
🟢 Approved
No active review issues remain for the changes intended to preserve slot identity and prevent state loss during re-renders.
No open issues 🎉
What changed in this PR
Fixes #1885 by preserving slot component identity when <Render> re-renders with unchanged inputs, preventing local state loss in rendered pages and the editor’s interactive preview.
Changes:
- Memoizes field transforms and slot-render callbacks.
- Stabilizes root component data and default metadata.
- Adds regression tests for state preservation in root, regular, and nested slots.
| File | Description |
|---|---|
packages/core/lib/props/render/use-render-props.tsx |
Memoizes combined transforms to preserve slot identity. |
packages/core/components/SlotRender/server.tsx |
Stabilizes nested slot-render callbacks. |
packages/core/components/Render/index.tsx |
Memoizes root data and callbacks; stabilizes default metadata. |
packages/core/components/Render/__tests__/index.spec.tsx |
Tests counter state preservation across re-renders at three slot depths. |
packages/core/components/DropZone/index.tsx |
Stabilizes slot-render callbacks for drop-zone items. |
Review effort: Balanced
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1885
Description
Slot content rendered by
<Render>is unmounted and mounted again on every re-render of<Render>, so any state inside a slot is lost. The editor's interactive preview renders through<Render>and is affected too. Edit mode is not, since #1480 and #1796 keep its transforms stable.useRenderPropsbuilt{ ...getSlotTransform(renderSlot), ...fieldTransforms }inline, and every caller passed a newrenderSlotarrow function on each render.useFieldTransformsmemoizes its mappers on that transforms object, so every render produced a newSlotcomponent for each slot prop, and React treated it as a different component type.This PR keeps the render path's slot components referentially stable when their inputs don't change, which is what #1480 did for the editor.
Changes made
useRenderPropsmemoizes its transforms onrenderSlotandfieldTransforms.DropZoneRenderItemandIteminSlotRenderwrap their slot render callbacks inuseCallbackonconfig,metadataandfieldTransforms.Renderbuilds the root item in auseMemoondata.rootandmetadata, and wraps its root slot callback inuseCallback, so root slot fields stay mounted too.Render's defaultmetadatais a module-level constant rather than a new{}on every render. Without it, every<Render>used withoutmetadatawould still remount its slots.useMemoanduseCallbackare both available in React'sreact-serverbuild, soSlotRender/server.tsxanduseRenderPropsremain RSC-safe.useFieldTransformsalready usesuseMemoon this path.Not covered here: slots still remount when their parent's data object changes identity, for example after any
setDatain the interactive preview, becauseuseRenderPropsuses the untrackeduseFieldTransforms. Switching to the tracked variant is a separate change, since it relies on refs, which RSC doesn't provide.How to test
components/Render/__tests__/index.spec.tsxrenders a page with a stateful counter in a root slot, in a slot, and in a nested slot. It increments each counter, re-renders<Render>with the sameconfiganddata, and asserts the count survives. All three cases fail onmainand pass with this change.<Puck ui={{ previewMode: "interactive" }}>, a counter inside a slot and one inside a nested slot now keep their state when<Puck>re-renders and when the selection changes. Onmainboth reset to 0. A top-level counter keeps its state either way.