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

React's strict mode causes the canvas to be created twice #255

Closed
neongreen opened this issue Jun 25, 2023 · 4 comments
Closed

React's strict mode causes the canvas to be created twice #255

neongreen opened this issue Jun 25, 2023 · 4 comments

Comments

@neongreen
Copy link

Related to #162.

I know that I can turn the strict mode off, but I'd rather fix the problem in p5-wrapper itself.

For reference: https://react.dev/reference/react/StrictMode. It causes all render methods to be called twice when dev mode is enabled.

It would seem that p5-wrapper doesn't use React's cleanup functionality properly -- it's supposed to remove the canvas when the component is unmounted.

Note that I can only reproduce this behavior in Chrome. I can provide a repro if this would help.

@jamesrweb
Copy link
Collaborator

jamesrweb commented Jun 25, 2023

Interesting...

We added cleanup effect handlers a while ago as you can see in the ReactP5WrapperWithSketch component.

I'm not sure why this is happening with the strict mode implementation. My only idea at the moment is that there's some effect race issue where the cleanup is done before the ref update. If that's the case then that's a React bug actually. I'm not entirely convinced that is the issue though.

Feel free to create a PR if you already have an idea of what it could be or if you want to take on a challenge? I'm always happy with support from the community. If you aren't able to contribute a PR then I'll take a look myself in the next few days 😁.

@neongreen
Copy link
Author

I am currently in the process of rewriting my code and honestly I might just be using refs/handles very wrongly. I’ll close this issue unless I have a clear repro.

@neongreen neongreen changed the title React's strict mode causes the canvas to be created twice [Chrome] React's strict mode causes the canvas to be created twice Jul 7, 2023
@neongreen
Copy link
Author

Actually no, I gave up on refs entirely and the issue persists. I'll provide a repro.

@neongreen neongreen reopened this Jul 7, 2023
@neongreen
Copy link
Author

Turns out that extensions are to blame.. although it's still unclear why it might be happening. And it can't be reproduced reliably.

When I enable a bunch of extensions at once, and especially if I also open devtools, I get two canvases perhaps 5 out of 10 times. If I enable a few extensions and don't open devtools — 1/10 times. I couldn't isolate a single extension that would be the culprit, though.

@neongreen neongreen closed this as not planned Won't fix, can't repro, duplicate, stale Jul 7, 2023
@neongreen neongreen changed the title [Chrome] React's strict mode causes the canvas to be created twice React's strict mode causes the canvas to be created twice Jul 7, 2023
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