简体   繁体   English

将基于promise的React应用程序转换为redux-saga:如何设置React组件变量?

[英]Converting promise-based React app to redux-saga: how do I set React component variables?

I'm a beginner with redux-saga and as a task I thought I'd try to convert a large existing React app from Promises to redux-saga. 我是redux-saga的初学者,作为一个任务,我认为我想尝试将现有的大型React应用程序从Promises转换为redux-saga。 I'm making some progress, but one issue I came on is that when I used Promises I was able to set component-local variables depending on whether the promise fulfilled or rejected. 我正在取得一些进展,但是我遇到的一个问题是,当我使用Promises时,我能够根据诺言是否实现来设置组件局部变量。 Now it seems I just "hand over" the request to redux-saga and never know (in the call site) whether it fulfilled or not, and now I don't know (within my component) whether it succeeded or not. 现在看来,我只是将请求“移交给” redux-saga,却不知道(在呼叫站点中)请求是否已实现,现在,我(在组件内部)不知道请求是否成功。

For example, let us say I have this promise-based call which fetches my user's games 例如,假设我们有一个基于承诺的通话,该通话可获取用户的游戏

getCurrentGamesForUser = (nickname, token) => {
    return logic.getGamesForUser(nickname, token)
      .then(currentGames => {
        this.needToUpdateGamesFlagFromSocketIO = false
        this.setState({currentGames})
        sessionStorage.setItem('currentGames', JSON.stringify(currentGames))
      })
      .catch(({message}) => this.onError(message))
  }

Here I'm setting the flag this.needToUpdateGamesFlagFromSocketIO -- as well as a sessionStorage variable -- if my promise succeeds. 如果我的诺言成功,那么我在这里设置标志this.needToUpdateGamesFlagFromSocketIO以及sessionStorage变量。

Now, as I understand it, I would convert this to 现在,据我了解,我将其转换为

 getCurrentGamesForUser = (nickname, token) => {
    this.props.dispatch(getCurrentGames(nickname,token))
  }

and this works fine. 而且效果很好。 If there's an error, that will be sent to the store from the saga and my component will reflect that. 如果有错误,它将被从传奇中发送到商店,而我的组件将反映出来。

But what do I do about the local flags and the session storage? 但是我该如何处理本地标志和会话存储? Do I need to add these to the store as well? 我还需要将这些添加到商店吗? That seems a messy way to go, unless I had a separate store for each component, a local store for each component, which also seems messy. 除非我为每个组件都有一个单独的存储,为每个组件有一个本地存储,否则这似乎很麻烦。 I saw some discussion of a similar topic here, https://github.com/redux-saga/redux-saga/issues/907 , but there doesn't seem to be an answer. 我在这里看到了类似主题的一些讨论, https://github.com/redux-saga/redux-saga/issues/907 ,但似乎没有答案。 I'm sure I'm missing the "redux way" of handling all this, and so would welcome any guidance. 我确定我错过了处理所有这些问题的“冗余方式”,因此欢迎您提供任何指导。

Redux-Saga is meant to offload all action based triggers outside component to a separate saga scope. Redux-Saga旨在将组件外部的所有基于动作的触发器卸载到单独的Saga范围。 So unfortunately there is no straightforward way to implement what you have requested. 因此,不幸的是,没有实现您所请求内容的简单方法。 Although there are some suggested workarounds in the issue tracker you have mentioned. 尽管您提到的问题跟踪器中有一些建议的解决方法。

The primary aim of redux-saga was ease of management of side affects by offloading it to a separate execution context. redux-saga的主要目的是通过将副作用卸载到单独的执行上下文中来简化对副作用的管理。 One of the tradeoff, is communication can only happen through component -> action -> saga -> store -> component . 权衡之一是通信只能通过component -> action -> saga -> store -> component Hence for the above requirement, I believe using redux-saga would be counterproductive. 因此,对于上述要求,我相信使用redux-saga将适得其反。

That said, you can always use redux-saga in combination with other promise / callback based methods such as redux-thunk . 也就是说,您始终可以将redux-saga与其他基于promise / callback的方法(例如redux-thunk

Usecases such as above would be better suited for callback based implementations while complete isolation of side effects to redux would be handled well with redux-saga . 诸如此类的用例将更适合基于回调的实现,而redux-saga可以很好地处理副作用,从而完全隔离了redux-saga

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

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