File tree 2 files changed +28
-13
lines changed
2 files changed +28
-13
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' @craftjs/core ' : patch
3
+ ---
4
+
5
+ Fix orphaned nodes when parsing from Frame's children in strict mode
Original file line number Diff line number Diff line change @@ -44,25 +44,35 @@ export const Frame: React.FC<React.PropsWithChildren<FrameProps>> = ({
44
44
initialData : data || json ,
45
45
} ) ;
46
46
47
+ const isInitialChildrenLoadedRef = useRef ( false ) ;
48
+
47
49
useEffect ( ( ) => {
48
50
const { initialChildren, initialData } = initialState . current ;
49
51
50
52
if ( initialData ) {
51
53
actions . history . ignore ( ) . deserialize ( initialData ) ;
52
- } else if ( initialChildren ) {
53
- const rootNode = React . Children . only (
54
- initialChildren
55
- ) as React . ReactElement ;
56
-
57
- const node = query . parseReactElement ( rootNode ) . toNodeTree ( ( node , jsx ) => {
58
- if ( jsx === rootNode ) {
59
- node . id = ROOT_NODE ;
60
- }
61
- return node ;
62
- } ) ;
63
-
64
- actions . history . ignore ( ) . addNodeTree ( node ) ;
54
+ return ;
55
+ }
56
+
57
+ // Prevent recreating Nodes from child elements if we already did it the first time
58
+ // Usually an issue in React Strict Mode where this hook is called twice which results in orphaned Nodes
59
+ const isInitialChildrenLoaded = isInitialChildrenLoadedRef . current ;
60
+
61
+ if ( ! initialChildren || isInitialChildrenLoaded ) {
62
+ return ;
65
63
}
64
+
65
+ const rootNode = React . Children . only ( initialChildren ) as React . ReactElement ;
66
+
67
+ const node = query . parseReactElement ( rootNode ) . toNodeTree ( ( node , jsx ) => {
68
+ if ( jsx === rootNode ) {
69
+ node . id = ROOT_NODE ;
70
+ }
71
+ return node ;
72
+ } ) ;
73
+
74
+ actions . history . ignore ( ) . addNodeTree ( node ) ;
75
+ isInitialChildrenLoadedRef . current = true ;
66
76
} , [ actions , query ] ) ;
67
77
68
78
return < RenderRootNode /> ;
You can’t perform that action at this time.
0 commit comments