Docs: added section in slices-pattern explaining how to affect multiple stores in one function (#1404)

* added section in slices-pattern.md explaining how to have a function change multiple stores together

* change in title and subtext for better understanding
This commit is contained in:
Joel Mathew Koshy 2022-11-05 13:27:19 +05:30 committed by GitHub
parent 8329bbfa5f
commit a1d228767f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -62,6 +62,37 @@ function App() {
export default App
```
### Updating multiple stores
You can update multiple stores, at the same time, in a single function.
```js
import { createBearSlice } from './bearSlice'
import { createFishSlice } from './fishSlice'
export const createBearFishSlice = (set) => ({
addBearAndFish: () => {
createBearSlice(set).addBear()
createFishSlice(set).addFish()
},
})
```
Combining all the stores together is the same as before.
```js
import create from 'zustand'
import { createBearSlice } from './bearSlice'
import { createFishSlice } from './fishSlice'
import { createBearFishSlice } from './createBearFishSlice'
export const useBoundStore = create((...a) => ({
...createBearSlice(...a),
...createFishSlice(...a),
...createBearFishSlice(...a),
}))
```
## Adding middlewares
Adding middlewares to a combined store is the same as with other normal stores.