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

docs: add migration example in v3 create context #1524

Merged
merged 3 commits into from
Jan 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion docs/previous-versions/zustand-v3-create-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ nav: 18
A special `createContext` is provided since v3.5,
which avoids misusing the store hook.

> **Note**: This function will be deprecated in v4 and removed in v5.
> **Note**: This function is deprecated in v4 and will be removed in v5. See [Migration](#migration).

```jsx
import create from 'zustand'
Expand Down Expand Up @@ -102,3 +102,30 @@ export default function App({ initialBears }) {
)
}
```

## Migration

Discussion: https://github.com/pmndrs/zustand/discussions/1276

Here's the diff showing how to migrate from v3 createContext to v4 API.

```diff
// store.ts
+ import { createContext, useContext } from "react";
- import create from "zustand";
- import createContext from "zustand/context";
+ import { createStore, useStore } from "zustand";

- const useStore = create((set) => ({
+ const store = createStore((set) => ({
bears: 0,
increasePopulation: () => set((state) => ({ bears: state.bears + 1 })),
removeAllBears: () => set({ bears: 0 })
}));

+ const MyContext = createContext()

+ export const Provider = ({ children }) = <MyContext.Provider value={store}>{children}</MyContext.Provider>;

+ export const useMyStore = (selector) => useStore(useContext(MyContext), selector);
```