Reproduction showcasing that useId()
doesn't work inside <Suspense>
.
git clone git@github.com:brillout/react-bug-useId-suspense
cd react-bug-useId-suspense/
pnpm install
pnpm run dev
As single line:
git clone git@github.com:brillout/react-bug-useId-suspense && cd react-bug-useId-suspense/ && pnpm install && pnpm run dev
Go to http://localhost:3000 and observe the following:
- The text
I'm lazy loaded...
never resolves toHello
, which is unexpected. - The
console.log()
loggingid: :r0:
, thenid: :r1:
, thenid: :r2:
, etc. In other words: the ID provided byuseId()
is not stable, which is unexpected. This is the root cause of why1.
doesn't work as expected.
The following commits confirms that the root problem is indeed useId()
not returning a stable ID when used inside <Suspense>
.
- 359df7 which replaces
const id = useId()
withconst id = 'some-static-id'
, then1.
works as expected. - ba224b replaces
<Suspense>
withuseState()
to showcase thatuseId()
then works as expected (i.e. returns a stable ID). This indicates that the problem is indeed caused by usinguseId()
inside<Suspense>
.