Day 25 - redux-saga 文件范例

Q_Q 没学完啦

Redux-saga 范例

import { createStore, applyMiddleware } from 'redux'
import createSagaMiddleware from 'redux-saga'

import reducer from './reducers'
import mySaga from './sagas'

// create the saga middleware
const sagaMiddleware = createSagaMiddleware()
// mount it on the Store
const store = createStore(
  reducer,
  applyMiddleware(sagaMiddleware)
)

// then run the saga
sagaMiddleware.run(mySaga)

// render the application

sagas.js

import { call, put, takeEvery, takeLatest } from 'redux-saga/effects'
import Api from '...'

// 工作的 Saga:当 action 是 USER_FETCH_REQUESTED 时被触发
function* fetchUser(action) {
   try {
      const user = yield call(Api.fetchUser, action.payload.userId);
      yield put({type: "USER_FETCH_SUCCEEDED", user: user});
   } catch (e) {
      yield put({type: "USER_FETCH_FAILED", message: e.message});
   }
}

/*
  在每次 dispatch `USER_FETCH_REQUESTED` action 时,启动 fetchUser。
  允许同时取得使用者。
*/
function* mySaga() {
  yield takeEvery("USER_FETCH_REQUESTED", fetchUser);
}

/*
  另外你也可以使用 takeLatest。

  但不允许同时取得使用者。当一个 fetch 已经在 pending 时,如果取得 dispatch「USER_FETCH_REQUESTED」,
  正在等待的 fetch 会被取消,只执行最新的发出的 USER_FETCH_REQUESTED。
*/
function* mySaga() {
  yield takeLatest("USER_FETCH_REQUESTED", fetchUser);
}

export default mySaga;

REF

https://neighborhood999.github.io/redux-saga/


<<:  [Day25] Scrum失败经验谈 – 与需求单位之间的断层

>>:  Day25 RCU 同步机制

资安扫描常见种类_几款免费白箱扫描工具推荐(Sonarqube,Puma Scan)

本篇已同步发表至个人部落格 https://coolmandiary.blogspot.com/20...

Day01 - 挑战前言

前言 今天是我挑战的第一天,这次挑战其实是一个偶然状况下,资深的前辈看到我正好开着铁人挑战赛的报名页...

在安装haproxy时,显示重启失败

在安装软件时有安装成功,但启动时失败,有人有遇过这种情形吗? ...

在投影片内文字选取位置插入等大图片_以字图取代文字【PowerPoint VBA】

Sub 在投影片内文字选取位置插入等大图片_以字图取代文字() '20210320 Dim sld ...

JS 逻辑运算子及函式预设值 DAY56

逻辑运算子 MDN : https://developer.mozilla.org/zh-TW/do...