redux-sage 简单理解

每当提到Dva的时候,总会提到这个概念,就去扒文章找到这个比较好理解的;

定义: 一个中间件,也是异步解决方案,可以用来代替redux-thunk;

用途: 当项目比较大的时候,异步操作在action中会显得混乱,此时用saga来作统一的异步处理;

好处: 避免回调地狱,代码比较整齐;

import { createStore, applyMiddleware } from 'redux'
import createSagaMiddleware from 'redux-sage';
import reducer from 'reducers';
import mySage from './sagas';

const  sagaMiddleware = createSagaMiddlaware();
const store = createStore(
   reducer,
   applyMiddleware(sagaMiddleware);
)
// then run the saga
sagaMiddleware(mySaga);

saga.js:
export function* helloSaga(){
   //console.log('hello sage')
   yield console.log('1');
   yield console.log('2');
}
var sagaGen = helloSage(); //写成对象形式,若直接helloSage().next()则结果一直是1,停留在第一个yiled后面
conosle.log(sagaGen.next())  //1
conosle.log(sagaGen.next())  //2

猜你喜欢

转载自www.cnblogs.com/naniandongzhi/p/10600034.html