Next dev with React 18, Always render twice
See original GitHub issueVerify canary release
- I verified that the issue exists in Next.js canary release
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 21.2.0: Sun Nov 28 20:28:41 PST 2021; root:xnu-8019.61.5~1/RELEASE_ARM64_T6000
Binaries:
Node: 16.14.0
npm: 8.5.5
Yarn: 1.22.17
pnpm: 6.31.0
Relevant packages:
next: 12.1.4
react: 18.0.0
react-dom: 18.0.0
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.
// the project is just result of `create-next-app`
// pages/index.js
import { useEffect, useState } from 'react'
export default function Home() {
console.log('render')
const [state, setState] = useState(() => {
console.log('state initialize');
return 'state';
})
useEffect(() => {
return () => {
console.log('unmounted')
}
}, [])
return (
<div>
Home
</div>
)
}
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
Issue Analytics
- State:
- Created a year ago
- Reactions:10
- Comments:22 (4 by maintainers)
Top Results From Across the Web
Why my nextjs component is rendering twice? - Stack Overflow
I found the answer in this thread. https://github.com/vercel/next.js/issues/35822. In shortly, This issue is about React 18 Strict Mode.
Read more >React 18 - Avoiding Use Effect Getting Called Twice
For React Hooks in React 18, this means a useEffect() with zero dependencies will be executed twice. Here is a custom hook that...
Read more >React Components rendered twice — any way to fix this?
The reason why this happens is an intentional feature of the React.StrictMode . It only happens in development mode and should help to...
Read more >Double render in react v18.0.0? : r/reactjs - Reddit
React 18 now has a thing called StrictMode which runs a bunch of React methods twice when in development. This is to help...
Read more >Strict Mode - React
By intentionally double-invoking methods like the component constructor, strict mode makes patterns like this easier to spot. Note: In React 17, React ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
React 18’s release blogpost talks about this behavior in section New Strict Mode Behaviors
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?