redux-saga学习(三)
redux-saga
- 安装redux-saga(参考githup 地址:链接描述)
- redux-saga简单使用
import { createStore ,applyMiddleware ,compose } from 'redux'; import createSagaMiddleware from 'redux-saga'; import mySaga from './sagas'; // import thunk from 'redux-thunk'; import reducer from './reducer'; 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(mySaga); export default store;
- 对应创建sagas.js,把对应的请求统一放在sagas.js来管理
import { put, takeEvery } from 'redux-saga/effects'; import { GET_LIST_DATA } from './actionTypes'; import { initList } from './actionCreators'; import axios from 'axios'; function* fetchUser(action) { try { let resData = yield axios.get('/list.json'); const data = resData.data.list; console.log(data); yield put(initList(data)) } catch (e) { console.log("网络请求失败") } } function* mySaga() { yield takeEvery(GET_LIST_DATA, fetchUser); } export default mySaga;
相关推荐
jiangcs0 2020-02-22
pepping 2017-11-23
田智文 2019-06-30
老干部的大前端 2019-06-29
jiaojsun 2019-06-29
jiaojsun 2019-06-28
taoweiquan00 2019-06-28
luojinxu0 2017-09-27
皖林 2019-06-28
田智文 2019-06-27
littlelittle00 2019-06-27
空谷足音 2019-06-26
空谷足音 2019-06-26
田智文 2019-06-26
taoweiquan00 2019-06-25
jiaojsun 2019-06-21
taoweiquan00 2019-06-21
littlelittle00 2019-06-21
littlelittle00 2018-03-10