Skip to content
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

Issues on initial page load on NextJS v13 #1529

Closed
GLips opened this issue Nov 2, 2022 · 6 comments
Closed

Issues on initial page load on NextJS v13 #1529

GLips opened this issue Nov 2, 2022 · 6 comments
Labels
help wanted Please someone help on this

Comments

@GLips
Copy link

GLips commented Nov 2, 2022

Not sure exactly what's going on here, but I've created a minimal repro on CodeSandbox that shows the issue.

The error is TypeError: Cannot read properties of null (reading 'useContext')

Cannot read properties of null (reading 'useContext')

Commenting out useAtom() in /components/Clock.js and replacing its variables with hardcoded values causes the page to render correctly:

//  const [{ lastUpdate, light }, setClock] = useAtom(clockAtom);
const lastUpdate = 0;
const light = false;
const setClock = (x) => {};

Once you've loaded the page, you can add the useAtom() call again and remove the hardcoded definitions and the page will work as expected. Possibly something related to SSR or hydration?

@dai-shi
Copy link
Member

dai-shi commented Nov 2, 2022

Sounds like related with pmndrs/zustand#1395 in zustand.

@dai-shi dai-shi added the help wanted Please someone help on this label Nov 2, 2022
@GLips
Copy link
Author

GLips commented Nov 2, 2022

Sounds like related with pmndrs/zustand#1395 in zustand.

I think you're right—I neglected to mention this only occurs when you add experimental: { appDir: true } in next.config.js, as described in pmndrs/zustand#1395

@CodeWithShreyans
Copy link

image
This is probably because features like useContext required for Jotai are only available in client components. You need to append 'use client'; on the top of the file.

@Caryyon
Copy link

Caryyon commented Dec 14, 2022

would it be as easy as adding "use client" to the Provider.tsx and maybe a few other client component files? Otherwise maybe a small babel plugin that would parse the file for "use client" and throw an error that explains the issue? happy to do the work for either, just new to jotai so would love a nudge in that right direction.

@thejus-r
Copy link

thejus-r commented Mar 7, 2023

Jack Herrington made a great video about this I think.. YouTube Video

We really have to think differently when using React Server Components.

@dai-shi
Copy link
Member

dai-shi commented Apr 10, 2023

Closing as a resolution is "use client".

@dai-shi dai-shi closed this as completed Apr 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Please someone help on this
Projects
None yet
Development

No branches or pull requests

5 participants