retejs / rete

JavaScript framework for visual programming
https://retejs.org
MIT License
10.17k stars 653 forks source link

All nodes are duplicated when React renders again (after a click on a Button for example) #523

Closed sbstnkll closed 1 year ago

sbstnkll commented 3 years ago

Hi,

I used your exact example at https://codesandbox.io/s/retejs-react-render-t899c?file=/src/index.js

I have implenented a Button with MaterialUI. As soon as I click on it (I think React then re-renderes), or when just another function is called, all nodes get duplicated.

How can I resolve this? Can I wrap the Editor in a React-component?

EDIT:

This is my exact code - codesandbox: https://codesandbox.io/s/hungry-cray-r2881?file=/src/App.js (You can replicate the error there - just click on the hamburger button)

Bildschirmfoto 2021-04-23 um 00 33 03

Thanks, sebastian

Ni55aN commented 3 years ago

Since the ref attribute has an inline callback, the createEditor function is triggered on every rerender. As the basic example doesn't have rerenders, it is not reproducible.

I have updated an example and added useRete hook: https://codesandbox.io/s/retejs-react-render-t899c I hope it can fix the issue both with the creation of the editor and its destroy on unmount.

export function useRete() {
  const [container, setContainer] = useState(null);
  const editorRef = useRef();

  useEffect(() => {
    if (container) {
      createEditor(container).then((value) => {
        editorRef.current = value;
      });
    }
  }, [container]);

  useEffect(() => {
    return () => {
      if (editorRef.current) {
        editorRef.current.destroy();
      }
    };
  }, []);

  return [setContainer];
}
rete-js[bot] commented 1 year ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 10 days.