const init = { dataList:[] };//初始化dataState
const [state,dispatch] = useReducer(reducer,init);
useEffect(() => {
//发送网络请求
axios.get("/user?ID=123").then(res => {
dispatch({
type:ACTION_TYPE.GETDATA,
payload: //后端返回的数据
})
})
},[]);
//使用 state.dataList 渲染页面
在reducer页面
function reducer(state,action){
switch(action.type){
case "getData":
return { //根据需求对数据做出操作,这里将返回的数据与初始化的数据合并
...state,
dataList:action.payload
};
}
export default reducer;