import { useState } from 'react';
export function useReducer(reducer, initialState) {
const [state, setState] = useState(initialState);
function dispatch(action) {
const nextState = reducer(state, action);
setState(nextState);
}
return [state, dispatch];
}
所以useReducer就是简单封装过的useState吗?有什么单用useState做不到的事情吗?实际在开发中面对一个组件有多个参数状态需要分别控制的时候是使用一组useState比较多还是一个对象然后用useReducer来控制不同字段比较多?
usestate, usereducer, React, State