1.首先在TodoList中写一个componentDidMount的生命周期函数
2.导入axios包
import axios from 'axios';
3.在componentDidMount的生命周期函数中写出异步获取数据的函数体
componentDidMount() {
axios.get('http://yapi.demo.qunar.com/mock/38353/app')
.then((res) =>{
alert(suscc);
})
.catch(alert(err));
}
4.接下来我们要改变store中的List的数据就必须定义action
5.我们在actionCreators中定义一个action,这个action要返回一个data的数据
import { CHANGE_INPUT_VALUE , ADD_TODO_ITEM , DELET_TODO_ITEM , INIT_LIST_ACTION } from './ActionTypes';
export const initListAction = (data) => ({
type : INIT_LIST_ACTION,
data
})
6.然后我们再ActioTypes中定义 INIT_LIST_ACTION这个常量
export const INIT_LIST_ACTION = 'init_list_action';
7.返回TodoList中写异步请求数据函数
componentDidMount() {
axios.get('http://yapi.demo.qunar.com/mock/38353/app')
.then((res) =>{
const data = res.data;
const action = initListAction(data)
store.dispatch(action);
})
.catch(alert(err));
}
8.接下来在reducer来处理这个action
if(action.type === INIT_LIST_ACTION) {
const newState = JSON.parse(JSON.stringify(state));
newState.List = action.data;
return newState;
}
9.这样store中的List数据就请求成功了
Redux 中发送异步请求获取数据
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 本文转载自https://segmentfault.com/q/1010000013345427/a-102000...
- 概述步骤 a. 添加Volley库,并封装Volley为单例模式。 b. 使用OkHttp实现 基于 com.an...
- 前言2016年大四上学期实习,参与爬虫系统开发,公司也遇到了如何获取ajax,vue,angularJS渲染的网页...
- 数据获取 vue-resource vue-resource axios 单来说,vue-resource就像jQ...