mirror of
https://github.com/pmndrs/zustand.git
synced 2025-12-08 19:45:52 +00:00
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:
parent
8329bbfa5f
commit
a1d228767f
@ -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.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user