-
Notifications
You must be signed in to change notification settings - Fork 27.8k
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
Next dev with React 18, Always render twice #35822
Comments
Is it possible that you are wrapping the App in StrictMode? That would explain that double render. I can reproduce the same double calling of the hooks in this simple demo -> https://codesandbox.io/s/late-leaf-k5qh9k?file=/components/Test.js when wrapping the App in StrictMode This is after full page reload: We have just run into a similar issue with the "useUpdateEffect" construct in our Next v12 + React v18 project. Is it expected? |
Confusing as it may be (it certainly is to me!) this seems to be the correct new behavior of strict mode in React 18. Effects, state initializers, renders (etc.) are called twice in dev mode when react is in strict mode. |
React 18's release blogpost talks about this behavior in section New Strict Mode Behaviors |
I think this is not correct behavior, the call order is weird. In blog post said
if it is correct strict mode behavior, it should be
but it's not. unmount should be called between render, and initialize state should be called once. Initializing lazy state twice is critical part in my case. heavy object is created first twice and then destroyed last once. it is hard to organize because second creation is occured before destroy(umount). it seems to create two page component in parallel |
Okay, I found a creation twice related issue facebook/react#20090 and it is correct to call initialize state function twice. Thank you all! |
Same issue on my side (dev/build) with version 12.1.4. This issue only happens when using React 18. From the React 18 upgrade guide, it seems that I cannot really fix it on my side as it is happening internally in next. I'm using a custom express server and don't have much configuration. I can clearly see the first render (no interaction, only html etc) and the bundle render (with interaction) which is rendering just below to the first render. It could be linked to the strict mode or a hydratation issue. I will try to change some configurations to see if I can find a fix. |
Same issue here with version 12.1.4 & React 18. I am getting the initial HTML bundle on top of the rehydration. You can navigate through the rehydration, however, the initial HTML on top remains. This is only happening on |
I'm running into this issue too and it's breaking my current JWT refresh implementation by causing a race condition where two refresh requests are happening simultaneously. I've put in a workaround using localstorage for now but it's very strange that my authorization provider component is rendering (mounting?) twice in React 18 even though I'm not using strict mode. |
It is intended in React 18 with Strict Mode (FYI: https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html#updates-to-strict-mode). You can find a workaround here (reactwg/react-18#18) |
If you are not using strict mode it should not happen, so there might be some other issue with your code, & disabling strict mode is not recommended, as it is helpful in catching errors early. |
These are the logs with Strict mode ON, React 18 & latest Next.js 12.1.5, you can also see grayed out duplicate render log if you have installed React dev tools & it will confirm that its done by React strict mode |
I'm facing the same issue |
@zeakd I did read about the new strict mode behavior and turns out we can avoid this by turning off the strict mode in next.config.js. Next.js docs ref for strictMode toggling Worked for me 😉 Additionally, you can read about what's new in the react 18 strict mode here. |
and when the app makes calls to an api, it will call 2 times. If I want to keep StrictMode will this always happen? |
@alexalannunes Yes, but it's just a dev mode thing and doesn't affect production build 😄 |
confused with 18 render twice |
when using strict mode in react without nextjs the console output is grayed out, why is it not in nextjs? |
This change should've been highlighted more! 😪 |
Its grayed out(with react devtools) in Nextjs too, at least for me, Also getting unmount log before 2nd render. Everything is working as expected. |
i retested it and ur right its grayed out but not inside a useEffect(idk why!) import { useState, useEffect } from 'react';
export default function App() {
console.log('render');
const [state, setState] = useState(() => {
console.log('state initialized');
return 'state';
});
useEffect(() => {
console.log('run useEffect');
return () => console.log('unmount/ cleanup Effect');
}, []);
return <div>hey</div>;
} also the second render is before the unmount, i get the same order with react and next! |
This is working as intended as pointed out by many. Please have a look at https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html#updates-to-strict-mode for the explanation. |
Verify canary release
Provide environment information
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
Hi.
When use
next dev
with react 18, page always rendered twice.even lazy state intialize twice, and unmount last. So I think it is not remounted, but create twice.
However,
next build && next start
is okay and render once.Thanks
Expected Behavior
Render, and Initialize Once with
next dev
To Reproduce
create-next-app
npm run dev
The text was updated successfully, but these errors were encountered: