flux-inspired-practice.md
This file
Path
docs/learn/guides/flux-inspired-practice.md
77 lines · 2.7 KiB · Markdown
Last changed by
Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9
History of this fileTake it
Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9
lines 1–77why, one step back1---2title: Flux inspired practice3nav: 194---56Although Zustand is an unopinionated library, we do recommend a few patterns.7These are inspired by practices originally found in [Flux](https://github.com/facebookarchive/flux),8and more recently [Redux](https://redux.js.org/understanding/thinking-in-redux/three-principles),9so if you are coming from another library, you should feel right at home.1011However, Zustand does differ in some fundamental ways,12so some terminology may not perfectly align to other libraries.1314## Recommended patterns1516### Single store1718Your applications global state should be located in a single Zustand store.1920If you have a large application, Zustand supports [splitting the store into slices](./slices-pattern.md).2122### Use `set` / `setState` to update the store2324Always use `set` (or `setState`) to perform updates to your store.25`set` (and `setState`) ensures the described update is correctly merged and listeners are appropriately notified.2627### Colocate store actions2829In Zustand, state can be updated without the use of dispatched actions and reducers found in other Flux libraries.30These store actions can be added directly to the store as shown below.3132Optionally, by using `setState` they can be [located external to the store](./practice-with-no-store-actions.md)3334```js35const useBoundStore = create((set) => ({36 storeSliceA: ...,37 storeSliceB: ...,38 storeSliceC: ...,39 updateX: () => set(...),40 updateY: () => set(...),41}))42```4344## Redux-like patterns4546If you can't live without Redux-like reducers, you can define a `dispatch` function on the root level of the store:4748```typescript49const types = { increase: 'INCREASE', decrease: 'DECREASE' }5051const reducer = (state, { type, by = 1 }) => {52 switch (type) {53 case types.increase:54 return { grumpiness: state.grumpiness + by }55 case types.decrease:56 return { grumpiness: state.grumpiness - by }57 }58}5960const useGrumpyStore = create((set) => ({61 grumpiness: 0,62 dispatch: (args) => set((state) => reducer(state, args)),63}))6465const dispatch = useGrumpyStore((state) => state.dispatch)66dispatch({ type: types.increase, by: 2 })67```6869You could also use our redux-middleware. It wires up your main reducer, sets initial state, and adds a dispatch function to the state itself and the vanilla api.7071```typescript72import { redux } from 'zustand/middleware'7374const useReduxStore = create(redux(reducer, initialState))75```7677Another way to update the store could be through functions wrapping the state functions. These could also handle side-effects of actions. For example, with HTTP-calls. To use Zustand in a non-reactive way, see [the readme](https://github.com/pmndrs/zustand#readingwriting-state-and-reacting-to-changes-outside-of-components).