美文网首页
redux 异步action

redux 异步action

作者: 书简_yu | 来源:发表于2019-05-04 17:19 被阅读0次

redux 异步action

yarn add redux-thunk

import thunkMiddleware from 'redux-thunk';
import { createStore, applyMiddleware } from 'redux';
import reducer form './reducer';

function reudcer(state=0, action){
    // ...
}

let store = createStore(
    reducer,
    applyMiddleware(thunkMiddleware)
)

function incrementAsync() {
    return dispatch => {
        setTimeout(() => { 
            // dispath一个action
            dispatch({type: 'ADD'});
        }, 1000);
    };
}

// dispatch上面那个异步函数
store.dispatch(incrementAsync());

// 这就是一个简单的demo

参考 Redux Thunk

api请求demo

function foo(){
    return function(dispatch){
        retrun axios('http://localhost:4000').then(
            data => dispatch({type: 'FERCH_DATA', data}),
            err => dispatch({type: 'FETCH_ERR', err}),
        )
    }
}

store.dispatch(foo());

// 上述foo可用箭头函数改写,实现柯里化
const foo = dispatch => axios('...').then(...);

相关文章

  • 高级

    异步 Action 通过 redux-thunk ,在 action 中 dispatch action ,可以是...

  • redux 异步action

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

  • redux-thunk作用

    redux-thunk 是一个比较流行的 redux 异步 action 中间件,比如 action 中有 ***...

  • 使用redux-thunk 中间件实现ajax数据请求

    redux-thunk 是一个比较流行的 redux 异步 action 中间件,比如 action 中有 set...

  • redux-thunk

    一、redux-thunk介绍redux-thunk用于处理异步action,同类型的有redux-saga 二、...

  • Redux 的中间件和异步操作

    一、同步和异步 首先讲一下在redux之中,Action的同步和异步的概念。 异步:Action发出之后,过一段时...

  • 一起来学点redux-saga

    1.概述 Redux-saga是一个用于管理 Redux 应用异步操作的中间件(又称异步action) 本质都是为...

  • redux异步编程

    redux本身是不支持异步编程的。但是用上redux之后,最好所有的异步操作都放在action中去做。这个时候就需...

  • [React Native Error]Unhandled JS

    使用Redux 进行异步操作报错 解决 使用自定义中间件(middleware)来支持异步 action。

  • 初识redux-saga

    最近项目用了dva,dva对于异步action的处理是用了redux-saga,故简单学习了下redux-saga...

网友评论

      本文标题:redux 异步action

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