本文共 880 字,大约阅读时间需要 2 分钟。
一、什么是redux-saga
redux-saga是一个用于管理redux应用异步操作的中间件,redux-saga通过创建sagas将所有异步操作逻辑收集在一个地方集中处理,可以用来代替redux-thunk中间件。 二、编写demo 推荐使用 三、上代码 整个流程:ui组件触发action创建函数 —> action创建函数返回一个action ------> action被传入redux中间件(被 saga等中间件处理) ,产生新的action,传入reducer-------> reducer把数据传给ui组件显示 -----> mapStateToProps ------> ui组件显示import {call, put} from 'redux-saga/effects'export function* fetchData(action){ try{ const data = yield call(Api.fetchUser, action.payload.url); yield put({type: "SUCCEEDED", data}); }catch(error) yield put({type: "FAILED", error}); };};
export function* helloSaga() { console.log('Hello Sagas!');}
// ...import { createStore, applyMiddleware } from 'redux'import createSagaMiddleware from 'redux-saga'//...import { helloSaga } from './sagas'const store = createStore( reducer, applyMiddleware(createSagaMiddleware(helloSaga)))// rest unchanged
《参考: 》
转载地址:http://voipn.baihongyu.com/