Skip to content

nextjs with zustand-v4 (zustand/context migration guide) #1180

Answered by dai-shi
benx-wang asked this question in General
Discussion options

You must be logged in to vote

Good point.
We have an example in https://github.com/pmndrs/zustand/releases/tag/v4.0.0, but not migration guide.

zustand/context migration guide

Previously, we did something like this:

import create from 'zustand'
import createContext from 'zustand/context'

const MyContext = createContext()

const createMyStore = () => create(...)

const App = () => (
  <MyContext.Provider createStore={createMyStore}>
    ...
  </MyContext.Provider>
)

const Component = () => {
  const slice = MyContext.useStore(selector)
  ...
}

With v4, it can be the following:

import { createStore, useStore } from 'zustand'
import { createContext, useContext } from 'react'

const MyContext = createContext()

const cr…

Replies: 5 comments 18 replies

Comment options

You must be logged in to vote
15 replies
@cuppachino
Comment options

@maxgurewitz
Comment options

@prabak
Comment options

@dai-shi
Comment options

@Munawwar
Comment options

Answer selected by benx-wang
Comment options

You must be logged in to vote
2 replies
@arvinxx
Comment options

@prabak
Comment options

Comment options

You must be logged in to vote
1 reply
@cuppachino
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet