Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sasa-djuric/zustand-slice
A zustand middleware that provides an API like createSlice from Redux toolkit.
https://github.com/sasa-djuric/zustand-slice
middleware react reactjs redux redux-toolkit slice state state-management zustand
Last synced: 3 months ago
JSON representation
A zustand middleware that provides an API like createSlice from Redux toolkit.
- Host: GitHub
- URL: https://github.com/sasa-djuric/zustand-slice
- Owner: sasa-djuric
- License: mit
- Created: 2021-12-24T17:41:50.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-03-07T00:25:17.000Z (almost 2 years ago)
- Last Synced: 2024-09-27T17:40:50.552Z (3 months ago)
- Topics: middleware, react, reactjs, redux, redux-toolkit, slice, state, state-management, zustand
- Language: TypeScript
- Homepage:
- Size: 605 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# zustand-slice
A [zustand](https://github.com/pmndrs/zustand) middleware that provides an API like [createSlice](https://redux-toolkit.js.org/api/createslice) from Redux toolkit.
## Install
```sh
npm install zustand-slice
```or
```sh
yarn add zustand-slice
```## Usage
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 one up;
}
```## Usage with typescript
By default, types will be inferred from initialState object and reducers payload, so there is no need to type the whole store explicitly.
## License
[MIT](https://github.com/sasa-djuric/zustand-slice/blob/master/LICENSE)