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

Update how to reset state doc #1495

Merged
merged 6 commits into from Dec 27, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 7 additions & 9 deletions docs/guides/how-to-reset-state.md
Expand Up @@ -44,20 +44,17 @@ const useSlice = create<State & Actions>((set, get) => ({
}))
```

Resetting multiple stores at once instead of individual stores
Resetting multiple stores at once

```ts
import _create, { StateCreator, StoreApi, UseBoundStore } from 'zustand'
import _create, { StoreMutatorIdentifier, StateCreator } from 'zustand'

const resetters: (() => void)[] = []

export const create = <TState extends unknown>(
createState: StateCreator<TState> | StoreApi<TState>
): UseBoundStore<StoreApi<TState>> => {
// We need to use createState as never to support StateCreator<TState> and
// StoreApi<TState> at the same time.
// We also need to re-type slice to UseBoundStore<StoreApi<TState>>
const slice: UseBoundStore<StoreApi<TState>> = _create(createState as never)
const create = <TState, Mos extends [StoreMutatorIdentifier, unknown][] = []>(
createState: StateCreator<TState, [], Mos>
) => {
const slice = _create<TState, Mos>(createState)
const initialState = slice.getState()

resetters.push(() => {
Expand All @@ -78,3 +75,4 @@ export const resetAllSlices = () => {

- Basic: https://codesandbox.io/s/zustand-how-to-reset-state-basic-demo-rrqyon
- Advanced: https://codesandbox.io/s/zustand-how-to-reset-state-advanced-demo-gtu0qe
- Immer: https://codesandbox.io/s/how-to-reset-state-advance-immer-demo-nyet3f
dbritto-dev marked this conversation as resolved.
Show resolved Hide resolved