vercel / next.js

The React Framework
https://nextjs.org
MIT License
124.95k stars 26.69k forks source link

`useEffect` no longer run twice in React strict mode #66210

Open chungweileong94 opened 3 months ago

chungweileong94 commented 3 months ago

Link to the code that reproduces this issue

https://github.com/chungweileong94/nextjs-strict-mode-useeffect-bug

To Reproduce

  1. Start the app in development mode
  2. Navigate the page

Current vs. Expected behavior

Expected:

Current:

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.5.0: Wed May  1 20:16:51 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T8103
  Available memory (MB): 16384
  Available CPU cores: 8
Binaries:
  Node: 20.11.1
  npm: 10.2.4
  Yarn: 1.22.21
  pnpm: 8.15.7
Relevant Packages:
  next: 14.3.0-canary.81 // Latest available version is detected (14.3.0-canary.81).
  eslint-config-next: N/A
  react: 19.0.0-beta-04b058868c-20240508
  react-dom: 19.0.0-beta-04b058868c-20240508
  typescript: 5.1.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Developer Experience

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

I tested the other strict mode behavior (double re-rendering), and it seems to be working. However, the double useEffect doesn't seem to work as expected according to the React documentation.

chungweileong94 commented 3 months ago

Probably not related, but I actually found this bug because one of my server toast hack stopped working during deployment in the NextJS 15 RC, where I was trying to read a maxAge: 0 cookie after a redirect. It was working before, so it got me thinking if some behavior changed that broke my hack and also the useEffect.

eps1lon commented 3 months ago

This is a bug in React that's already been reported. Double invocation currently doesn't work during hydration in the React 19 RC. Mounts during client-side updates will double invoke Effects in Strict Mode.