-
Notifications
You must be signed in to change notification settings - Fork 46.4k
-
Notifications
You must be signed in to change notification settings - Fork 46.4k
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
Bug: confused with react 18 render twice #24425
Comments
how to create a xterm once if render twice? |
React 18 renders your component twice in development mode. This is done to detect problems with purity. Your component has to be pure. You shouldn't change any variables that were created before your component. See react docs: https://beta.reactjs.org/learn/keeping-components-pure In your case, you defined Remember, given the same input, react component should always produce the same exact output. In your code, this is not the case, as when the component re-renders, it could give different output depending on the current value of So, by rendering your whole component twice, react makes impure issues easier to detect. Which is exactly what's happening in your code here. Also, please don't forget to remove your event listener in the return statement of the useEffect. Otherwise, if the component is unmounted and remounted again, you will just keep adding event listeners causing performance issues down the line. |
Get it. thanks, but it's difficult for most people to write absolutely pure components in a large application. It's a strongly mental burden. And can i have much more demos with your best practices or faq? I think it's a long way to write pure components with most people. I rewrite my code and I sure it's more rigorous, here it's my resolution, render twice but create one terminal with useRef.
React 18 with strict mode is a good way to write pure component, I get a lot, thank you for your work |
someone else can see this to solve your problem |
To be clear, this behavior only happens in development in Strict Mode. But we recommend to keep it on because it tests whether your component is resilient to remounting with existing state: https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html#updates-to-strict-mode We plan to add a feature to React that would let a component preserve state between unmounts. So to verify that your component works with it, we mount it, unmount and immediately remount with existing state. The behavior you see is the behavior that would happen in this case. So cleaning up effects and making them symmetric is a good idea. I am not sure the solution you wrote in #24425 (comment) is the best. I think you want to ensure there is a symmetry in your effects: whatever gets created, also gets destroyed in cleanup. And then the next time effect runs, the terminal is created again. So ideally what should happen is: (1) terminal is created, (2) that terminal is destroyed, (3) terminal is created again. And then it would not be visible because the first terminal was silently created and destroyed. This would simulate what should happen if the component remounts. Indeed, reactwg/react-18#18 should provide more guidance. |
We wrote a new page documenting this in detail on the Beta website. Hope this is helpful. Sorry some links from there are 404 because other pages are still being written. |
Awesome Explanation , Issue is cleared |
For me this caused Firefox to crush the application in production mode. Chrome just fine |
React version:18.0.0
with 18
with 17
and my app demo
code is ok with react17
I'm very confused, may I get your help?
The text was updated successfully, but these errors were encountered: