redux.md
This file
Path
docs/reference/middlewares/redux.md
170 lines · 3.9 KiB · Markdown
Last changed by
docs(react): add Action constraint to redux middleware signature (#3492)Seydi Charyyevbefore adoptionfd8c601
History of this fileTake it
Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9
lines 1–27why, one step back1---2title: redux3description: How to use actions and reducers in a store4nav: 305---67# redux89`redux` middleware lets you update a store through actions and reducers just like redux.1011```js12const nextStateCreatorFn = redux(reducerFn, initialState)13```1415- [Types](#types)16 - [Signature](#signature)17 - [Mutator](#mutator)18- [Reference](#reference)19- [Usage](#usage)20 - [Updating state through actions and reducers](#updating-state-through-actions-and-reducers)21- [Troubleshooting](#troubleshooting)2223## Types2425### Signature2627```tsdocs(react): add Action constraint to redux middleware signature (#3492)Seydi Charyyevbefore adoptionfd8c601
line 28why, one step back28redux<T, A extends { type: string }>(reducerFn: (state: T, action: A) => T, initialState: T): StateCreator<T & { dispatch: (action: A) => A }, [['zustand/redux', A]], []>Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9
lines 29–33why, one step back29```3031### Mutator3233```tsUpgrade workflow to use build.yml v3 and deploy-pages v4 (#3393)Danilo Brittobefore adoption49ac892
line 34why, one step back34;['zustand/redux', A]Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9
lines 35–170why, one step back35```3637## Reference3839### `redux(reducerFn, initialState)`4041#### Parameters4243- `reducerFn`: It should be pure and should take the current state of your application and an action44 object as arguments, and returns the new state resulting from applying the action.45- `initialState`: The value you want the state to be initially. It can be a value of any type,46 except a function.4748#### Returns4950`redux` returns a state creator function.5152## Usage5354### Updating state through actions and reducers5556```ts57import { createStore } from 'zustand/vanilla'58import { redux } from 'zustand/middleware'5960type PersonStoreState = {61 firstName: string62 lastName: string63 email: string64}6566type PersonStoreAction =67 | { type: 'person/setFirstName'; firstName: string }68 | { type: 'person/setLastName'; lastName: string }69 | { type: 'person/setEmail'; email: string }7071type PersonStore = PersonStoreState & {72 dispatch: (action: PersonStoreAction) => PersonStoreAction73}7475const personStoreReducer = (76 state: PersonStoreState,77 action: PersonStoreAction,78) => {79 switch (action.type) {80 case 'person/setFirstName': {81 return { ...state, firstName: action.firstName }82 }83 case 'person/setLastName': {84 return { ...state, lastName: action.lastName }85 }86 case 'person/setEmail': {87 return { ...state, email: action.email }88 }89 default: {90 return state91 }92 }93}9495const personStoreInitialState: PersonStoreState = {96 firstName: 'Barbara',97 lastName: 'Hepworth',98 email: 'bhepworth@sculpture.com',99}100101const personStore = createStore<PersonStore>()(102 redux(personStoreReducer, personStoreInitialState),103)104105const $firstNameInput = document.getElementById(106 'first-name',107) as HTMLInputElement108const $lastNameInput = document.getElementById('last-name') as HTMLInputElement109const $emailInput = document.getElementById('email') as HTMLInputElement110const $result = document.getElementById('result') as HTMLDivElement111112function handleFirstNameChange(event: Event) {113 personStore.dispatch({114 type: 'person/setFirstName',115 firstName: (event.target as any).value,116 })117}118119function handleLastNameChange(event: Event) {120 personStore.dispatch({121 type: 'person/setLastName',122 lastName: (event.target as any).value,123 })124}125126function handleEmailChange(event: Event) {127 personStore.dispatch({128 type: 'person/setEmail',129 email: (event.target as any).value,130 })131}132133$firstNameInput.addEventListener('input', handleFirstNameChange)134$lastNameInput.addEventListener('input', handleLastNameChange)135$emailInput.addEventListener('input', handleEmailChange)136137const render: Parameters<typeof personStore.subscribe>[0] = (person) => {138 $firstNameInput.value = person.firstName139 $lastNameInput.value = person.lastName140 $emailInput.value = person.email141142 $result.innerHTML = `${person.firstName} ${person.lastName} (${person.email})`143}144145render(personStore.getInitialState(), personStore.getInitialState())146147personStore.subscribe(render)148```149150Here's the `html` code151152```html153<label style="display: block">154 First name:155 <input id="first-name" />156</label>157<label style="display: block">158 Last name:159 <input id="last-name" />160</label>161<label style="display: block">162 Email:163 <input id="email" />164</label>165<p id="result"></p>166```167168## Troubleshooting169170TBD