Skip to content

fix(joint-react): tolerate a cell removed mid-render (useCell) and assign the forwarded ref during commit (useCombinedRef) - #3447

Open
samuelgja wants to merge 1 commit into
clientIO:devfrom
samuelgja:fix/use-combined-ref-and-usecells
Open

fix(joint-react): tolerate a cell removed mid-render (useCell) and assign the forwarded ref during commit (useCombinedRef)#3447
samuelgja wants to merge 1 commit into
clientIO:devfrom
samuelgja:fix/use-combined-ref-and-usecells

Conversation

@samuelgja

Copy link
Copy Markdown
Contributor

Description

Two independent React-hook lifecycle fixes in @joint/react, each with a jest repro. Both are reachable through ordinary, documented usage and both are masked by StrictMode's effect remount, so they only surfaced in production builds.

1. useCell — tolerate a cell removed mid-render (fixes #3442)

useCell() threw useCell(): no cell with id "X" and took the whole paper down when a subscribed cell was removed from a controlled cells array (renaming a node id is a remove + add). A renderElement subtree that subscribes to its own cell — directly, or via useMeasureElement -> useCell(selectElementSize) for any content-sized node, i.e. the default HTMLBox / SVGText renderers — re-runs its selector against the already-removed cell before <Paper> reconciles the removal, because the controlled apply fires that cell's per-id listener synchronously in a layout effect.

The wrapper selector now keeps the last value it resolved for the current id (one in-place-mutated ref, no per-render allocation) and returns it during that removed-mid-render window: the doomed subtree renders once more with stale data (equal to the cached value, so it does not even re-render) and unmounts on the next commit, instead of throwing. A cell that never resolved (genuine bad-id misuse / outside a render context) still throws.

Repro: use-cell-controlled-removal.test.tsx — a real Paper + subscribing renderElement doing a controlled b -> c rename crashes to 0 elements before the fix, survives (2 elements, no error) after.

2. useCombinedRef — assign the forwarded ref during commit, not in a passive effect (fixes #3444)

useCombinedRef assigned the forwarded ref inside a passive useEffect, one phase too late. React attaches refs during commit, before layout effects; a parent that reads the forwarded ref in its own useLayoutEffect saw null. useMeasureElement is exactly that consumer — it reads nodeRef.current in a layout effect, bails on null, and its dependency list does not change when the node is finally assigned, so a measured element stayed 0x0 in production.

The hook now returns a proxy whose setter forwards during commit (setForwardRef runs when React writes .current), keeping the RefObject .current read interface consumers depend on — which a plain callback ref could not.

Repro: use-combined-ref-timing.test.tsx (rendered with reactStrictMode: false) — a parent layout effect sees null before the fix, the node after.

Motivation and Context

The useCell crash came out of a Mermaid-to-JointJS renderer where every keystroke in a node id took the canvas down; the useCombinedRef one from an app measuring SVGText directly — correct under vite dev, every node a bare label with no shape or layout once built for production. StrictMode (on for every story and test) masks both, which is why the suite was blind to them.

Fixes #3442
Fixes #3444

🤖 Generated with Claude Code

https://claude.ai/code/session_01F3YAR6SSWn39wTNhBTyFGX

…ensure correct behavior during controlled updates
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.

1 participant