A zustand middleware that provides an API like createSlice from Redux toolkit.
npm install zustand-sliceA zustand middleware that provides an API like createSlice from Redux toolkit.
``sh`
npm install zustand-slice
or
`sh`
yarn add zustand-slice
Create a store using slice middleware.
`js
import create from 'zustand';
import slice from 'zustand-slice';
const useStore = create(
slice({
initialState: {
bears: 0
},
reducers: {
increasePopulation(state) {
return {
bears: state.bears + 1
};
},
removeAllBears() {
return {
bears: 0
};
}
}
})
);
`
And use it as usual.
`jsx
function BearCounter() {
const bears = useStore(state => state.bears);
return {bears} around here ...
;
}
function Controls() {
const increasePopulation = useStore(state => state.increasePopulation);
return ;
}
``
By default, types will be inferred from initialState object and reducers payload, so there is no need to type the whole store explicitly.