-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Zustand breaks with NextJS 13 when enabling appDir (Cannot read properties of null (reading 'useDebugValue')) #1395
Comments
Does anyone like me think this might have something to do with the "Dual Package Hazard" caused by multiple instances of a same dependency?
|
Zustand library itself shouldn't suffer from Dual Package Hazard, because it doesn't have module state. (On the other hand, Jotai and Valtio have module state, so they can suffer.) |
@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 diff --git a/node_modules/zustand/esm/index.mjs b/node_modules/zustand/esm/index.mjs
index 2c76b55..73ad140 100644
--- a/node_modules/zustand/esm/index.mjs
+++ b/node_modules/zustand/esm/index.mjs
@@ -13,7 +13,9 @@ function useStore(api, selector = api.getState, equalityFn) {
selector,
equalityFn
);
- useDebugValue(slice);
+ if (typeof window !== "undefined") {
+ useDebugValue(slice);
+ }
return slice;
}
const createImpl = (createState) => { |
Ah, maybe because React doesn't provide ESM. // ./node_modules/zustand/esm/index.mjs
import React from 'react'
// ...
React.useDebugValue(slice) |
nope, this won't work, same error. |
These two issues might be relevant? (but neither solves this problem) |
I start thinking that this issue may be a misuse of the nextjs 13 |
Same issue without appDir is enabled |
Hi everyone! Just want to note that we're looking into this on the nextjs team. |
@jescalan Good news! Anywhere we can track progress of the investigation? |
No, it's generally in our company slack. We will have something out for this soon though, it's an active priority. The core issue is the ESM thing mentioned above, which is also causing bugs with a number of other things. |
@jescalan any suggestions on state management tools similar to Zustand in the mean time? |
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: contact.tsx
|
@nhatimme Have you tried adding |
@adriannecris Thanks. So it is impossible to use it as Server Side component? |
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 |
@jescalan: Perfect. |
still having this issue im 13.0.6 now |
can use it just fine after add "use client" |
you may be able to use useBearStore.setState(...) / useBearStore.getState().myVal in the server side without an issue. |
all client based hook should be used after adding 'use client' on top of the file. But Next13 does not give a good error message where is missing. That is really tough since I have to find it manually. |
@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:
|
it could be because of a lot of reason and next 13 sometimes gives not even relative error. worst part is it ia hard to tell where is the origin of the error.. you might want to move all page and app path to somewhere else not in the app directory to narrow down where is the origin of the issue. |
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: vercel/next.js#49355 (comment) We've fixed this for now by specifying our Next.js version as |
It seems like this issue is fixed with |
it is working fine when I tried it you can see it below example: |
I think this issue should be closed. |
Zustand has been working fine in my nextjs project with app dir, so I'm closing this issue now. |
still getting same error, on
|
Issue seems to appear again with next |
Did you try deleting |
Please try #2154 |
it was fast! Thanks! |
Issue not fixed yet it seems. I face the issue with |
@JanRuettinger update Zustand to the latest version |
This issue appears again on |
Issue is here again on |
I created a minimal reproduction here:
https://stackblitz.com/edit/nextjs-egfcaj?file=package.json
It throws the following error:
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: #1392 but it didn't help.
Relevant NextJS discussion: vercel/next.js#41236
The text was updated successfully, but these errors were encountered: