redux-saga和redux-thunk一样,都是redux的中间件
1.安装
npm install --save redux-saga
2.在store下面index.js中写入
import { createStore, applyMiddleware, compose } from 'redux'
import reducer from './reducer'
import mySaga from './sagas'
import createSagaMiddleware from 'redux-saga'
const sagaMiddleware=createSagaMiddleware()
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION__ ?
window.__REDUX_DEVTOOLS_EXTENSION__({}) : compose
const enhancer = composeEnhancers(applyMiddleware(sagaMiddleware))
const store = createStore(reducer,enhancer)
sagaMiddleware.run(mySaga)
export default store;
3.在当前目录中新建sagas.js文件
import {takeEvery,put} from 'redux-saga/effects'
import {GET_MY_LIST} from './actionTypes'
import axios from 'axios'
import {getListAction} from './actionCreators'
function* mySaga(){
yield takeEvery(GET_MY_LIST,getList)
}
function* getList(){
const res =yield axios.get('https://www.easy-mock.com/mock/5cfcce489dc7c36bd6da2c99/xiaojiejie/getList')
const action =getListAction(res.data)
yield put(action)
}
export default mySaga
PS:这也是我在bilibili上看视频学习的,前端新手一枚