简体   繁体   English

为什么要使用redux-thunk或redux-saga来获取?

[英]Why use redux-thunk or redux-saga for fetches?

I keep reading that I should use redux-thunk or redux-saga to handle side effects. 我一直在读,我应该使用redux-thunk或redux-saga来处理副作用。 Why not simply use action creators like that to dispatch multiple actions : 为什么不简单地使用这样的动作创建者来发送多个动作:

function loadProductActionCreator(dispatch) {
  dispatch({
    type: 'load_product',
  })
  fetch('api/product').then(
    function (r) {
      return r.json();
    }
  )
  .then(function (res) {
    dispatch({
      type: 'loaded_product',
      data: res
    })
  })
}

I tried that and it worked ( complete code ). 我试过了,它工作( 完整的代码 )。 So I guess there must be some inconvenients I'm not aware of. 所以我想一定有一些我不知道的不便之处。

You code is similar to what thunk does. 你的代码类似于thunk的代码。

As per redux docs, actions should be pure. 根据redux docs,行动应该是纯粹的。 And they should always return same values for same input parameters. 并且它们应始终为相同的输入参数返回相同的值。 By using fetch you are allowing action to return not specific value, rather value from server and that mean action response may vary upon time. 通过使用fetch您允许操作返回非特定值,而不是返回服务器的值,这意味着操作响应可能会随时间而变化。

That is called side effects . 这被称为副作用 And it's something what shouldn't be in redux actions by default. 并且这是默认情况下不应该在redux操作中的内容。

But why? 但为什么?

Yes, you can type it inside action like you have, in small apps it does not matter. 是的,你可以像你一样在里面输入它,在小应用程序中它并不重要。

In larger application there are benefits of using redux-saga : 在更大的应用程序中,使用redux-saga有好处:

  • actions are predictable, they just return payload like 动作是可预测的,它们只返回有效载荷

     { type: 'FETCH_POSTS', params: { category: 'programming' } } 
  • and then you build middleware which will take actions with all data required to perform request to real API 然后构建中间件,该中间件将对执行实际API请求所需的所有数据执行操作

Possible advantages: 可能的优点:

  • Cleaner codebase (but may be overhead on smaller applications) 更清洁的代码库(但可能是较小的应用程序的开销)
  • Separation of "dummy" actions with all required information to perform requests and actual API middleware 将“虚拟”操作与所有必需信息分离以执行请求和实际API中间件
  • Request parameters are visible directly in redux dev tools 请求参数可直接在redux dev工具中查看
  • Possible to easily debounce , throttle fetches which may be really tricky with redux-thunk 可以轻松debouncethrottle使用redux-thunk可能非常棘手的提取
  • Possible to easily combine actions (wait for another event/fetch, chain events) 可以轻松组合操作(等待另一个事件/获取,链事件)
  • Possible to stop running tasks 可以停止运行任务

From personal experience, on one project (larger codebase) we have started with redux-thunk , but later we needed to integrate more advanced features, like throttle, and some dependencies between actions. 从个人经验来看,在一个项目(更大的代码库)上我们已经开始使用redux-thunk ,但后来我们需要集成更高级的功能,比如节流,以及操作之间的一些依赖关系。 So we rewrote everything to redux-saga and it worked well for us. 因此我们将所有内容重写为redux-saga并且它对我们来说效果很好。

You are kind of replicating redux-thunk here. 你在这里复制redux-thunk。 A pure redux action creator should return an action object to be dispatched and not dispatch an action itself (see redux doc on action creator ). 纯redux动作创建者应返回要调度的动作对象,而不是自行调度动作(请参阅动作创建者的redux doc )。

To better understand why your technic is a replication of redux-thunk, look at this post from its author 为了更好地理解为什么你的技术是redux-thunk的复制,请查看其作者的这篇文章

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

相关问题 哪个最好使用Redux-thunk或redux-saga - Which one is best to use Redux-thunk or redux-saga Redux-Thunk到Redux-Saga - Redux-Thunk to Redux-Saga React-Redux有几种使用方法? Redux-thunk vs Redux-Saga? - How many methods are there to use React-Redux? Redux-thunk vs Redux-Saga? 将关于redux-thunk的操作复制到redux-saga - Replicating the action on redux-thunk to redux-saga 如何在另一个 redux-saga 结束时调用一个 saga 就像在 redux-thunk 中异步等待一样 - How to call one saga at the end of another redux-saga like a async await in redux-thunk 关于没有中间件的Redux Async(redux-thunk,redux-saga ...) - About Redux Async without middleware (redux-thunk, redux-saga… ) 一种在不使用 redux-thunk 或 redux-saga 中间件的情况下处理 Redux 存储上的异步状态更新的方法? - A way to handle async state updates on Redux store without using redux-thunk or redux-saga middleware? 在中间件即 redux-thunk 或 redux-saga 中运行异步代码有什么好处 - What is the advantage of running asynchronous code in middleware i.e. redux-thunk or redux-saga 使用带有 ES6 生成器的 redux-saga 与带有 ES2017 async/await 的 redux-thunk 的优缺点 - Pros/cons of using redux-saga with ES6 generators vs redux-thunk with ES2017 async/await 使用redux-thunk和redux-observable - use of redux-thunk with redux-observable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM