redux-saga
是一個用於管理應用程序反作用(例如異步獲取數據,訪問瀏覽器緩存等)的javascript庫,它的目標是讓反作用管理更容易,執行更高效,測試更簡單,處理故障更容易。javascript
redux-saga
至關於一個放置在action
與reducer
中的墊片。java
之因此稱之謂反作用呢,就是爲了避免讓觸發一個action
時,當即執行reducer
。也就是在action
與reducer
之間作一個事情,好比異步獲取數據等。react
redux-saga
使用了ES6中的Generator
功能,避免了像redux-thunk
的回調地獄。git
$ npm install --save redux-saga //或者 $ yarn add redux-saga
假設有一個UI
界面,是根據用戶ID
顯示用戶詳情的。那麼咱們須要經過接口從數據庫根據userId
來獲取數據。github
簡單起見,咱們在本地使用一個json
文件來模擬數據庫數據。數據庫
{ "297ee83e-4d15-4eb7-8106-e1e5e212933c": { "username": "Saga" } }
建立UI Componentnpm
import React from 'react'; import { USER_FETCH_REQUESTED } from '../../../actions/User'; export default class extends React.Component { constructor(props) { super(props); this.state = { userId: '297ee83e-4d15-4eb7-8106-e1e5e212933c' } } render() { const { userInfo = {}, dispatch } = this.props; return ( <React.Fragment> <button onClick={() => { dispatch({ type: USER_FETCH_REQUESTED, payload: { userId: this.state.userId } }); }}>Get User Info</button> <span>用戶名: {userInfo.username}</span> </React.Fragment> ); } }
建立saga
,這裏的saga
就至關於action
.json
import { call, put, takeEvery } from 'redux-saga/effects' import { fetchUserApi } from '../api/user'; import { USER_FETCH_REQUESTED, USER_FETCH_SUCCEEDED, USER_FETCH_FAILED } from '../actions/User'; function* fetchUser({ payload }) { try { const user = yield call(fetchUserApi, payload.userId); yield put({ type: USER_FETCH_SUCCEEDED, user }); } catch (e) { yield put({ type: USER_FETCH_FAILED, message: e.message }); } } const userSaga = function* () { yield takeEvery(USER_FETCH_REQUESTED, fetchUser); } export default userSaga;
關於fetchUserApi
,咱們會在後面的章節中描述。這裏僅獲取了json
文件中與userId
相對應的數據。redux
把saga放入store
中:api
import createSagaMiddleware from 'redux-saga'; import Sagas from '../sagas/index'; const sagaMiddleware = createSagaMiddleware() const store = createStore( reducer, applyMiddleware(sagaMiddleware) ) sagaMiddleware.run(Sagas)
最後再實現相就的reducer便可:
import { USER_FETCH_SUCCEEDED } from '../actions/User'; const initialState = { user: {} }; export default (state = initialState, action) => { switch (action.type) { case USER_FETCH_SUCCEEDED: return { ...state, user: action.user }; default: return state; } }
推薦閱讀React 手稿