question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Next dev with React 18, Always render twice

See original GitHub issue

Verify 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>
  )
}
스크린샷 2022-04-02 오전 12 36 06

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:closed
  • Created a year ago
  • Reactions:10
  • Comments:22 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
MonstraGcommented, Apr 1, 2022

React 18’s release blogpost talks about this behavior in section New Strict Mode Behaviors

6reactions
GLObus303commented, Apr 1, 2022

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: CleanShot 2022-04-01 at 18 03 24

We have just run into a similar issue with the “useUpdateEffect” construct in our Next v12 + React v18 project. Is it expected?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found