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
    }

 

posted @ 2022-06-21 23:52  SimoonJia  阅读(89)  评论(0编辑  收藏  举报