Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sweet-terms-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xyflow/react': patch
---

Use `useEffect` for StoreUpdater to restore previous behaviour
12 changes: 3 additions & 9 deletions packages/react/src/components/StoreUpdater/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
* We distinguish between values we can update directly with `useDirectStoreUpdater` (like `snapGrid`)
* and values that have a dedicated setter function in the store (like `setNodes`).
*/
import { useRef } from 'react';
import { useEffect, useRef } from 'react';
import { shallow } from 'zustand/shallow';
import { infiniteExtent, type CoordinateExtent, mergeAriaLabelConfig, AriaLabelConfig } from '@xyflow/system';

import { useIsomorphicLayoutEffect } from '../../hooks/useIsomorphicLayoutEffect';
import { useStore, useStoreApi } from '../../hooks/useStore';
import type { Node, Edge, ReactFlowState, ReactFlowProps, FitViewOptions } from '../../types';
import { defaultNodeOrigin } from '../../container/ReactFlow/init-values';
Expand Down Expand Up @@ -126,12 +125,7 @@ export function StoreUpdater<NodeType extends Node = Node, EdgeType extends Edge
} = useStore(selector, shallow);
const store = useStoreApi<NodeType, EdgeType>();

// We use layout effects here so that the store is always populated before
// any child useEffect or useLayoutEffect fires. With regular useEffect, the
// cleanup calls reset() which empties the store, and child effects can run
// before the new mount effect repopulates it — causing children to read
// empty nodeLookup/nodes/edges during a <ReactFlow> remount.
useIsomorphicLayoutEffect(() => {
useEffect(() => {
setDefaultNodesAndEdges(props.defaultNodes, props.defaultEdges);

return () => {
Expand All @@ -143,7 +137,7 @@ export function StoreUpdater<NodeType extends Node = Node, EdgeType extends Edge

const previousFields = useRef<Partial<StoreUpdaterProps<NodeType, EdgeType>>>(initPrevValues);

useIsomorphicLayoutEffect(
useEffect(
() => {
for (const fieldName of fieldsToTrack) {
const fieldValue = props[fieldName];
Expand Down
Loading