zustand: Zustand breaks with NextJS 13 when enabling appDir (Cannot read properties of null (reading 'useDebugValue'))
I created a minimal reproduction here: https://stackblitz.com/edit/nextjs-egfcaj?file=package.json
It throws the following error:
Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
TypeError: Cannot read properties of null (reading 'useDebugValue')
at Module.useDebugValue (/home/projects/nextjs-egfcaj/node_modules/react/cjs/react.development.js:1659:23)
at useStore (file:///home/projects/nextjs-egfcaj/node_modules/zustand/esm/index.mjs:37:10)
at useBoundStore (file:///home/projects/nextjs-egfcaj/node_modules/zustand/esm/index.mjs:42:51)
at __WEBPACK_DEFAULT_EXPORT__ (webpack-internal:///./pages/index.js:20:19)
If you remove the “appDir: true” line from next.config.js, the counter works as expected. The weirdest thing is that the project doesn’t even use the app dir, just enabling the flag is enough for the error to happen. After a bit of debugging in my project, comparing the useDebugValue that is imported by zustand and the one I imported in my component, they are not equal. Seems like the multiple react copies error, although there is only one react version installed.
I also tried zustand version 3, it throws a similar error, but with the useReducer hook instead of useDebugValue. I also tried the version from here: https://github.com/pmndrs/zustand/issues/1392 but it didn’t help.
Relevant NextJS discussion: https://github.com/vercel/next.js/discussions/41236
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 23
- Comments: 39 (4 by maintainers)
Hi everyone! Just want to note that we’re looking into this on the nextjs team.
Yeah, it’s never going to be possible to use zustand in a server component, as state is not supported by server components in general. It works quite well in app directory right now as long as you are using client components. Actually I think it may be working fine with the latest version of nextjs, I just made a reproduction repo here and everything is working quite nicely. This includes an example of it working both in pages and app directory.
Let me know if I missed anything or if anyone has a reproduction with the latest version - hopefully this can be closed out!
Edit: confirmed that updating to
13.0.3
in the original stackblitz fixes the issueIssue seems to appear again with next
13.4.19
still getting same error, on
pages
directory. Using following version of next and zustand:Same issue without appDir is enabled
Issue is here again on
"next": "14.1.0", "zustand": "^4.5.0"
It seems like this issue is fixed with
next@13.4.3
for custom server (my use case). This issue is tracked in Next.js https://github.com/vercel/next.js/issues/49355, it is not related to just zustand. I think this issue should be closed.We encountered this error when using Next.js 13.4 with a custom server, even though we were not using the new app dir. I believe the problem is explained here: https://github.com/vercel/next.js/issues/49355#issuecomment-1537536063
We’ve fixed this for now by specifying our Next.js version as
~13.3.4
@vlausargs Which files are you labelling ‘use client’? I have tried adding it to all files that import anything from zustand and I’m still seeing the error appearing:
you may be able to use useBearStore.setState(…) / useBearStore.getState().myVal in the server side without an issue.
can use it just fine after add “use client”
@nhatimme Have you tried adding
'use client';
at the very first line. Reference in this linkPlease try #2154 https://ci.codesandbox.io/status/pmndrs/zustand/pr/2154 ☝️ Find “Local Install Instructions”
We also facing a issue when we want to load the store into a file which is located at /app/contact.tsx (It gives an error like:
Unhandled Runtime Error Error: useRef is not a function
)contact.tsx
@jescalan any suggestions on state management tools similar to Zustand in the mean time?
nope, this won’t work, same error.
@dai-shi yeah you’re right it is not caused by zustand library itself. I did some test and I saw this error is triggered when
useDebugValue
is called on the server side. This simple patch will make the code work. Not sure about the proper way to fix this though: