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

Should not set the useRef current value during rendering #4690

Closed
raRaRa opened this issue Mar 29, 2024 · 2 comments
Closed

Should not set the useRef current value during rendering #4690

raRaRa opened this issue Mar 29, 2024 · 2 comments

Comments

@raRaRa
Copy link

raRaRa commented Mar 29, 2024

What docs page needs to be fixed?

What is the problem?

The suggested way of storing the store in a useRef does not follow the guidelines from React. That is, we are not supposed to set the value of current during rendering.

Do not write or read ref.current during rendering.

Here's the piece of code from the documentation:

const storeRef = useRef<AppStore | null>(null)
if (!storeRef.current) {
  storeRef.current = makeStore()
  storeRef.current.dispatch(initializeCount(count))
}

What should be changed to fix the problem?

Not quite sure. I managed to create the store outside the component just fine with NextJS client component. Unsure what side-effects that will have. But it seems to work just fine since nothing in the store is SSR or RSC.

@EskiMojo14
Copy link
Contributor

The documentation you're quoting says

Do not write or read ref.current during rendering, except for initialization.

if you read the section on initialization, that's exactly what we're doing here.

@phryneas
Copy link
Member

Also, definitely do not create a store for usage in Next.js outside of a component. Your components will SSR and this can have all kinds of weird consequences.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants