#redux
redux 中间件让你可以像 Redux 那样,通过 action 和 reducer 来更新 store。
const nextStateCreatorFn = redux(reducerFn, initialState)#类型
#签名
redux<T, A extends { type: string }>(reducerFn: (state: T, action: A) => T, initialState: T): StateCreator<T & { dispatch: (action: A) => A }, [['zustand/redux', A]], []>#Mutator
;['zustand/redux', A]#参考
#redux(reducerFn, initialState)
#参数
reducerFn:它应当是纯函数,接收应用的当前状态和一个 action 对象作为参数,并返回应用该 action 后得到的新状态。initialState:你希望状态最初取的值。它可以是任意类型的值,但不能是函数。
#返回值
redux 返回一个 state creator 函数。
#用法
#通过 action 和 reducer 更新状态
import { createStore } from 'zustand/vanilla'
import { redux } from 'zustand/middleware'
type PersonStoreState = {
firstName: string
lastName: string
email: string
}
type PersonStoreAction =
| { type: 'person/setFirstName'; firstName: string }
| { type: 'person/setLastName'; lastName: string }
| { type: 'person/setEmail'; email: string }
type PersonStore = PersonStoreState & {
dispatch: (action: PersonStoreAction) => PersonStoreAction
}
const personStoreReducer = (state: PersonStoreState, action: PersonStoreAction) => {
switch (action.type) {
case 'person/setFirstName': {
return { ...state, firstName: action.firstName }
}
case 'person/setLastName': {
return { ...state, lastName: action.lastName }
}
case 'person/setEmail': {
return { ...state, email: action.email }
}
default: {
return state
}
}
}
const personStoreInitialState: PersonStoreState = {
firstName: 'Barbara',
lastName: 'Hepworth',
email: '[email protected]',
}
const personStore = createStore<PersonStore>()(redux(personStoreReducer, personStoreInitialState))
const $firstNameInput = document.getElementById('first-name') as HTMLInputElement
const $lastNameInput = document.getElementById('last-name') as HTMLInputElement
const $emailInput = document.getElementById('email') as HTMLInputElement
const $result = document.getElementById('result') as HTMLDivElement
function handleFirstNameChange(event: Event) {
personStore.dispatch({
type: 'person/setFirstName',
firstName: (event.target as any).value,
})
}
function handleLastNameChange(event: Event) {
personStore.dispatch({
type: 'person/setLastName',
lastName: (event.target as any).value,
})
}
function handleEmailChange(event: Event) {
personStore.dispatch({
type: 'person/setEmail',
email: (event.target as any).value,
})
}
$firstNameInput.addEventListener('input', handleFirstNameChange)
$lastNameInput.addEventListener('input', handleLastNameChange)
$emailInput.addEventListener('input', handleEmailChange)
const render: Parameters<typeof personStore.subscribe>[0] = (person) => {
$firstNameInput.value = person.firstName
$lastNameInput.value = person.lastName
$emailInput.value = person.email
$result.innerHTML = `${person.firstName} ${person.lastName} (${person.email})`
}
render(personStore.getInitialState(), personStore.getInitialState())
personStore.subscribe(render)下面是 html 代码
<label style="display: block">
First name:
<input id="first-name" />
</label>
<label style="display: block">
Last name:
<input id="last-name" />
</label>
<label style="display: block">
Email:
<input id="email" />
</label>
<p id="result"></p>#疑难解答
待补充