Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get correct access to a Konva node with useEffect in StrictMode #761

Open
Kiku-CN opened this issue Jul 25, 2023 · 1 comment
Open

Comments

@Kiku-CN
Copy link

Kiku-CN commented Jul 25, 2023

There is demo. I expect that transformer always attach to react whatever count is, but transformer disappear when increase count to 3 by click +1 button. Everything work fine without StrictMode.

I tried to make some explanation what happen.

React Strict Mode rerender component in development. useEffect will also run twice when the first mount,so the ref in effect can get corresponding ref to knova node when count initialize with 1.

Component still render twice when count increase, but the effect only run once due to same count between two render. the ref in effect refer to konva node in first render, but the konva really show on the screen seems to be the second render. So the transormer disappear.

@lavrton
Copy link
Member

lavrton commented Jul 26, 2023

That is an interesting issue. I see that on count = 3 it creates several new instances of all konva nodes. But somehow in effect in references to the old instances. If I do the effect in timeout, it resolves the issue:

React.useEffect(() => {
    setTimeout(() => {
      trRef.current?.nodes([shapeRef.current]);
      trRef.current?.getLayer().batchDraw();
    }, 100)
  }, [count]);

I am not sure yet what will be solution from react-konva side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants