我只想知道是否可能在 useEffect 中使用 useReducer,将获取的数据传递给 State,然后使用 useReducer(..., State)。
const [initialData, setInitialData] = useState({ name: 'ass' });
const [data, dispatch] = useReducer(apiReducer, initialData);
const Data2 = useFetch('/qaz')
useEffect(() => {
setInitialData(Data2)
}, [Data2])
useEffect(() => {
dispatch({ type: 'Different', payload: 'key' })
}, [initialData])
export function apiReducer(state, action) {
switch (action.type) {
case 'Different':
return { ...state, key: action.payload };
default:
return state
}
}