1.npm install redux-saga
2.store/index的配置
引入createSagaMiddleware,创建SagaMiddleware
通过applyMiddleware使用中间件
创建sagas.js文件
引入sagas.Js文件
sagas.Js文件的书写
通过sagamiddleware运行sagas.js文件

import createSagaMiddleware from 'redux-saga';
import { createStore, compose, applyMiddleware } from 'redux';
import reducer from './reducer';
import todoSagas from './sagas';

const sagaMiddleware = createSagaMiddleware();
const composeEnhancers =
  window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
    }) : compose;
const enhancer = composeEnhancers(
  applyMiddleware(sagaMiddleware)
);

const store = createStore(reducer, enhancer);

sagaMiddleware.run(todoSagas);
export default store;

2.当做了配置后派发action时

  componentDidMount() {
    const action = getInitList();
    store.dispatch(action);
  }

不仅reducer能接受到派发的action,而且在sagas中也能接收到派发的action
react简书项目学习笔记25redux-saga中间件
sagas中一定要导出generaotor函数
function* 函数名(){

}

sogas在处理非常大型项目的时候由于redux-thunk,但是其api比较复杂,redux-thunk比较简单(使得我们返回的action可以是函数)

相关文章:

  • 2021-12-23
  • 2022-12-23
  • 2021-12-09
  • 2021-11-28
  • 2022-12-23
  • 2022-02-10
  • 2021-08-12
猜你喜欢
  • 2021-05-24
  • 2021-11-05
  • 2021-12-08
  • 2021-11-05
  • 2021-08-20
  • 2022-03-04
  • 2021-11-12
相关资源
相似解决方案