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: swap immer and computec #28

Merged
merged 2 commits into from
Aug 22, 2024
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
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,24 @@ A fully-featured example can be found under the "example" directory.
Here's an example with the Immer middleware.

> [!WARNING]
> Types may not be as you expect when using Immer, as it derives the SetState type from the output of GetState, where `zustand-computed` makes SetState only allow the regular Store and the GetState return both the store and the computed store. To access the ComputedStore inside Immer, you will need to assert the `Store` type as `Store & ComputedStore`.
> Immer derives the SetState type from the output of GetState, where `zustand-computed` types SetState to allow only the regular Store and types GetState to return both the store and the computed store. To avoid this issue, you may need to apply Immer outside of `zustand-computed`. If `zustand-computed` must be outside of Immer, you will need to assert the `Store` type as `Store & ComputedStore`.

```ts
const useStore = create<Store>()(
devtools(
computed(
immer((set) => ({
count: 1,
inc: () =>
set((state) => {
// example with Immer middleware
state.count += 1
}),
dec: () => set((state) => ({ count: state.count - 1 })),
})),
computeState
immer(
computed(
(set) => ({
count: 1,
inc: () =>
set((state) => {
// example with Immer middleware
state.count += 1
}),
dec: () => set((state) => ({ count: state.count - 1 })),
}),
computeState
),
)
)
)
Expand Down
Loading