Skip to content

fix: don't remount slot content when Render re-renders - #1886

Open
gui-com-pt wants to merge 5 commits into
puckeditor:mainfrom
gui-com-pt:fix/render-slot-remount
Open

gui-com-pt wants to merge 5 commits into
puckeditor:mainfrom
gui-com-pt:fix/render-slot-remount

Conversation

@gui-com-pt

Copy link
Copy Markdown

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.

useRenderProps built { ...getSlotTransform(renderSlot), ...fieldTransforms } inline, and every caller passed a new renderSlot arrow function on each render. useFieldTransforms memoizes its mappers on that transforms object, so every render produced a new Slot component 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

  • useRenderProps memoizes its transforms on renderSlot and fieldTransforms.
  • DropZoneRenderItem and Item in SlotRender wrap their slot render callbacks in useCallback on config, metadata and fieldTransforms.
  • Render builds the root item in a useMemo on data.root and metadata, and wraps its root slot callback in useCallback, so root slot fields stay mounted too.
  • Render's default metadata is a module-level constant rather than a new {} on every render. Without it, every <Render> used without metadata would still remount its slots.
  • useMemo and useCallback are both available in React's react-server build, so SlotRender/server.tsx and useRenderProps remain RSC-safe. useFieldTransforms already uses useMemo on this path.

Not covered here: slots still remount when their parent's data object changes identity, for example after any setData in the interactive preview, because useRenderProps uses the untracked useFieldTransforms. 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.tsx renders 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 same config and data, and asserts the count survives. All three cases fail on main and pass with this change.
  • In jsdom, with <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. On main both reset to 0. A top-level counter keeps its state either way.

@vercel

vercel Bot commented Oct 29, 2026

Copy link
Copy Markdown

@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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Slot content remounts every time <Render> re-renders

2 participants