美文网首页
Redux 中发送异步请求获取数据

Redux 中发送异步请求获取数据

作者: 路人_Ding | 来源:发表于2019-01-11 16:21 被阅读0次
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 中发送异步请求获取数据

  • Redux中发送异步请求获取数据

    步骤 首先,引入axios,在生命周期函数componentDidMount中发送请求。 获取到数据后,通过sto...

  • vue中用async/await 来处理异步

    用async/ await来发送异步请求,从服务端获取数据,等待获取数据,然后处理数据。 resolve,reje...

  • 08_Ajax&Json

    异步请求 无刷新获取服务器资源。特点: js发送异步请求,服务器响应返回的数据给到js,js操作dom更新页面 无...

  • React解惑之 redux-saga

    Redux-saga 概述 redux-saga和redux-thunk一样,是用于处理redux应用异步请求的中...

  • 爬虫的概念

    爬虫是模拟浏览器发送请求,获取响应 爬虫的流程 url--->发送请求,获取响应--->提取数据---》保存数据 ...

  • 9.数据的异步ajax

    前言 这节我们将实现数据的异步获取,主要用到了: redux-async-connect:数据异步取,并将状态放入...

  • redux 异步action

    redux 异步action yarn add redux-thunk 参考 Redux Thunk api请求demo

  • redux-saga

    redux-saga 一个用于管理Redux应用异步操作的中间件,使副作用(数据获取、浏览器缓存获取)易于管理、运...

  • 同步请求&异步请求

    同步:发送http请求→获取返回结果→分析结果→跳转下一页(主UI线程卡死,等待返回结果) 异步:发送http请求...

网友评论

      本文标题:Redux 中发送异步请求获取数据

      本文链接:https://www.haomeiwen.com/subject/vbbxdqtx.html