JotaiJotai

Jotai
React 原始而灵活的状态管理

Reducer

atomWithReducer

参考: https://github.com/pmndrs/jotai/issues/38

import { atomWithReducer } from 'jotai/utils'
const countReducer = (prev, action) => {
if (action.type === 'inc') return prev + 1
if (action.type === 'dec') return prev - 1
throw new Error('unknown action type')
}
const countReducerAtom = atomWithReducer(0, countReducer)

useReducerAtom

参见 useReducerAtom 食谱。