zustandjs / zustand-slices

A slice utility for Zustand
MIT License
137 stars 6 forks source link

How to use it with immer? #20

Open shahreaz0 opened 1 month ago

shahreaz0 commented 1 month ago

Like this Screenshot_2024-05-25-20-33-41-74_f9ee0578fe1cc94de7482bd41accb329.jpg

Screenshot_2024-05-25-20-35-52-53_f9ee0578fe1cc94de7482bd41accb329.jpg

dai-shi commented 1 month ago

This is probably opinionated, but I'd suggest to use produce.

import { create } from 'zustand';
import { createSlice, withSlices } from 'zustand-slices';
import { produce } from 'immer';

const countSlice = createSlice({
  name: 'count',
  value: 0,
  actions: {
    inc: () => produce((draft) => { ++draft }),
    reset: () => () => 0,
  },
});