Skip to content

Commit

Permalink
chore(docs): update docs content
Browse files Browse the repository at this point in the history
  • Loading branch information
dbritto-dev committed Nov 2, 2024
1 parent c87a5d6 commit 040e73a
Show file tree
Hide file tree
Showing 3 changed files with 1,444 additions and 73 deletions.
78 changes: 7 additions & 71 deletions docs/guides/how-to-reset-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ const useSlice = create<State & Actions>()((set, get) => ({
Resetting multiple stores at once

```ts
import { create as _create } from 'zustand'
import type { StateCreator } from 'zustand'
import type * as zustand from 'zustand'
import { create: actualCreate } from 'zustand'

const storeResetFns = new Set<() => void>()

Expand All @@ -55,80 +55,16 @@ const resetAllStores = () => {
})
}

export const create = (<T extends unknown>() => {
return (stateCreator: StateCreator<T>) => {
const store = _create(stateCreator)
const initialState = store.getState()
export const create = (<T>() => {
return (stateCreator: zustand.StateCreator<T>) => {
const store = actualCreate(stateCreator)
const initialState = store.getInitialState()
storeResetFns.add(() => {
store.setState(initialState, true)
})
return store
}
}) as typeof _create
```

Resetting bound store using Slices pattern

```ts
import create from 'zustand'
import type { StateCreator } from 'zustand'

const sliceResetFns = new Set<() => void>()

export const resetAllSlices = () => {
sliceResetFns.forEach((resetFn) => {
resetFn()
})
}

const initialBearState = { bears: 0 }

interface BearSlice {
bears: number
addBear: () => void
eatFish: () => void
}

const createBearSlice: StateCreator<
BearSlice & FishSlice,
[],
[],
BearSlice
> = (set) => {
sliceResetFns.add(() => set(initialBearState))
return {
...initialBearState,
addBear: () => set((state) => ({ bears: state.bears + 1 })),
eatFish: () => set((state) => ({ fishes: state.fishes - 1 })),
}
}

const initialStateFish = { fishes: 0 }

interface FishSlice {
fishes: number
addFish: () => void
}

const createFishSlice: StateCreator<
BearSlice & FishSlice,
[],
[],
FishSlice
> = (set) => {
sliceResetFns.add(() => set(initialStateFish))
return {
...initialStateFish,
addFish: () => set((state) => ({ fishes: state.fishes + 1 })),
}
}

const useBoundStore = create<BearSlice & FishSlice>()((...a) => ({
...createBearSlice(...a),
...createFishSlice(...a),
}))

export default useBoundStore
}) as typeof zustand.create
```

## CodeSandbox Demo
Expand Down
6 changes: 4 additions & 2 deletions docs/guides/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ In the next steps we are going to setup our Jest environment in order to mock Zu

```ts
// __mocks__/zustand.ts
import * as zustand from 'zustand'
import { act } from '@testing-library/react'
import type * as zustand from 'zustand'
export * from 'zustand'

const { create: actualCreate, createStore: actualCreateStore } =
jest.requireActual<typeof zustand>('zustand')
Expand Down Expand Up @@ -172,8 +173,9 @@ In the next steps we are going to setup our Vitest environment in order to mock
```ts
// __mocks__/zustand.ts
import * as zustand from 'zustand'
import { act } from '@testing-library/react'
import type * as zustand from 'zustand'
export * from 'zustand'

const { create: actualCreate, createStore: actualCreateStore } =
await vi.importActual<typeof zustand>('zustand')
Expand Down
Loading

0 comments on commit 040e73a

Please sign in to comment.