|
1 | 1 | import { deprecationWarning, ROOT_NODE } from '@craftjs/utils'; |
2 | | -import React, { useEffect, useRef } from 'react'; |
| 2 | +import React, { useRef } from 'react'; |
3 | 3 |
|
4 | 4 | import { useInternalEditor } from '../editor/useInternalEditor'; |
5 | 5 | import { SerializedNodes } from '../interfaces'; |
@@ -39,41 +39,28 @@ export const Frame: React.FC<React.PropsWithChildren<FrameProps>> = ({ |
39 | 39 | }); |
40 | 40 | } |
41 | 41 |
|
42 | | - const initialState = useRef({ |
43 | | - initialChildren: children, |
44 | | - initialData: data || json, |
45 | | - }); |
| 42 | + const isLoaded = useRef(false); |
46 | 43 |
|
47 | | - const isInitialChildrenLoadedRef = useRef(false); |
48 | | - |
49 | | - useEffect(() => { |
50 | | - const { initialChildren, initialData } = initialState.current; |
| 44 | + if (!isLoaded.current) { |
| 45 | + const initialData = data || json; |
51 | 46 |
|
52 | 47 | if (initialData) { |
53 | 48 | actions.history.ignore().deserialize(initialData); |
54 | | - return; |
55 | | - } |
| 49 | + } else if (children) { |
| 50 | + const rootNode = React.Children.only(children) as React.ReactElement; |
56 | 51 |
|
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; |
| 52 | + const node = query.parseReactElement(rootNode).toNodeTree((node, jsx) => { |
| 53 | + if (jsx === rootNode) { |
| 54 | + node.id = ROOT_NODE; |
| 55 | + } |
| 56 | + return node; |
| 57 | + }); |
60 | 58 |
|
61 | | - if (!initialChildren || isInitialChildrenLoaded) { |
62 | | - return; |
| 59 | + actions.history.ignore().addNodeTree(node); |
63 | 60 | } |
64 | 61 |
|
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; |
76 | | - }, [actions, query]); |
| 62 | + isLoaded.current = true; |
| 63 | + } |
77 | 64 |
|
78 | 65 | return <RenderRootNode />; |
79 | 66 | }; |
0 commit comments