简体   繁体   English

redux和redux-saga UI操作触发多个API操作

[英]redux and redux-saga UI action triggering multiple API actions

Just trying to understand what the best practice is for a bunch of API calls to depend on a single UI action in redux and redux-saga . 只是想了解一堆API调用的最佳实践是什么,它们依赖于reduxredux-saga的单个UI操作。 For instance 例如

  1. UI action SELECTED_ITEM_CHANGE gets triggered UI操作SELECTED_ITEM_CHANGE被触发
  2. Several API request actions get triggered: DATA1_REQUEST , DATA2_REQUEST , ..., DATA10_REQUEST 触发了几个API请求操作: DATA1_REQUESTDATA2_REQUEST ,..., DATA10_REQUEST
  3. When DATA1_REQUEST , ... , DATA10_REQUEST get resolved, redux-saga sends a bunch of actions like DATA1_SUCCESS with data to resolve to the store. DATA1_REQUEST ,..., DATA10_REQUEST得到解析时,redux-saga将诸如DATA1_SUCCESS之类的DATA1_SUCCESS与要解析的数据一起发送到存储中。 I got this part using redux-saga and I basically understand how to do it. 我使用redux-saga获得了这一部分,我基本上了解了如何做。

My question is, how do I get DATA1_REQUEST , ... , DATA10_REQUEST to get triggered when SELECTED_ITEM_CHANGE gets triggered? 我的问题是,当SELECTED_ITEM_CHANGE被触发时,如何获取DATA1_REQUEST ,..., DATA10_REQUEST来触发? The dumb way is to just do it in the component that sends SELECTED_ITEM_CHANGE and just dispatch all of those actions along with the SELECTED_ITEM_CHANGE action, but this is not modular since the component should not be responsible for all those API calls. 愚蠢的方法是只在发送SELECTED_ITEM_CHANGE的组件中执行此操作,然后dispatch所有这些动作与SELECTED_ITEM_CHANGE动作一起dispatch ,但这不是模块化的,因为该组件不应对所有这些API调用负责。 So who should handle this? 那么谁来处理呢? I guess there's a couple options: 我猜有两种选择:

  1. Make the action creator that dispatches SELECTED_ITEM_CHANGE also dispatch DATA1_REQUEST , ..., DATA10_REQUEST . 使调度SELECTED_ITEM_CHANGE的动作创建者也调度DATA1_REQUEST ,..., DATA10_REQUEST
  2. Make a saga that listens to SELECTED_ITEM_CHANGE and dispatch DATA1_REQUEST , ..., DATA10_REQUEST 做一个传奇,监听SELECTED_ITEM_CHANGE并调度DATA1_REQUEST ,..., DATA10_REQUEST

I'm leaning towards the second, but I'm not very sure. 我倾向于第二个,但是我不太确定。

Thanks for your help! 谢谢你的帮助!

Option 2 is your option, that's the whole point behind redux-saga AFAIK. 选项2是您的选择,这是redux-saga AFAIK的全部要点。 You can simply use takeEvery (would allow multiple requests) or takeLatest to listen for SELECTED_ITEM_CHANGE action and yield to another saga that actually performs the network requests. 您可以简单地使用takeEvery (将允许多个请求)或takeLatest来侦听SELECTED_ITEM_CHANGE动作,并屈服于实际执行网络请求的另一个传奇。

You can see an example right in the documentation here: https://redux-saga.js.org/docs/basics/UsingSagaHelpers.html 您可以在这里的文档中看到一个示例: https : //redux-saga.js.org/docs/basics/UsingSagaHelpers.html

Excerpt: 摘抄:

import { takeLatest } from 'redux-saga/effects'

function* fetchData(){
   try {
      const data = yield call(Api.fetchUser, action.payload.url)
      yield put({type: "FETCH_SUCCEEDED", data})
   } catch (error) {
      yield put({type: "FETCH_FAILED", error})
   }
}

function* watchFetchData() {
  yield takeLatest('FETCH_REQUESTED', fetchData)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM