Axios异步获取数据并和Redux结合
作者:互联网
TodoList.js
componentDidMount(){ store.subscribe(this.storeChange) //订阅 axios.get('https://........').then((res)=>{ const data=res.data const action=getListAction(data) store.dispatch(action) }) }
actionCreators.js
export const getListAction=(data)=>({ type:GET_LIST, data })
actionTypes.js
export const GET_LIST='getList'
reducer.js //因为axios是写componentDidMount中,所以该方法可以代替默认值呈现,默认值可设为空
if(action.type===GET_LIST){ let newState=JSON.parse(JSON.stringify(state))//深拷贝 newState.List=action.data.data.list return newState }
标签:Axios,const,GET,newState,js,获取数据,action,Redux,data 来源: https://www.cnblogs.com/Jarsmine/p/16398859.html