Graphin icon indicating copy to clipboard operation
Graphin copied to clipboard

Graphin doesn't work with global state library Jotai

Open eakl opened this issue 3 years ago • 4 comments
trafficstars

Describe the bug

Using global state library such as Jotai or Zustend causes the following error:

TypeError: Cannot read properties of undefined (reading 'manipulateBehaviors')

However, using useState works as expected.

const [layout] = useAtom(layoutAtom);
// versus
const [layout] = useState("graphin-force");

Full code below:

import Graphin, { Utils, Behaviors } from "@antv/graphin";
import { atom, useAtom } from "jotai";
import "./styles.css";

const { ZoomCanvas } = Behaviors;

const data = Utils.mock(10).circle().graphin();
const layoutAtom = atom("graphin-force");

export default function App() {
  const [layout] = useAtom(layoutAtom);
  // const [layout] = useState("graphin-force");
  
  return (
    <div className="App">
      <Graphin data={data} layout={{ type: layout }}>
        <ZoomCanvas disabled />
      </Graphin>
    </div>
  );
}

Your Example Website or App

https://codesandbox.io/s/graphin-jotai-g2n4je?file=/src/App.tsx

Expected behavior

Graphin should work with Jotai

eakl avatar Sep 29 '22 05:09 eakl

Seems like useEffect is causing problem

In the following code, <LayoutSelector /> calls setLayout() with the new layout

Without useEffect, the code works, with useEffect, it fails with the same error as above.

export default function App() {
  const [layout, setLayout] = useState('random')

  useEffect(() => {
    console.log('New Layout: ', layout)
  }, [layout])
  
  return (
    <div className="App">
      <Graphin data={data} layout={{ type: layout }}>
        <ZoomCanvas disabled />
        <LayoutSelector setLayout={setLayout} />
      </Graphin>
    </div>
  );
}

eakl avatar Oct 03 '22 16:10 eakl

Has someone been able to test it?

eakl avatar Oct 11 '22 13:10 eakl

This issue is related to this discussion: https://github.com/antvis/Graphin/discussions/415

eakl avatar Oct 14 '22 17:10 eakl

This can be an issue with React Strict mode. The graph is not rendered properly in strict mode. Note that in strict mode, components are rendered twice. Ref: https://stackoverflow.com/questions/61254372/my-react-component-is-rendering-twice-because-of-strict-mode

You may try to disable strict mode and see if the error disappears.

mikaelfs avatar Nov 03 '22 01:11 mikaelfs