处理redux中的复杂异步动作,并处理状态差异

发布于 2025-01-24 06:11:36 字数 362 浏览 0 评论 0 原文

假设我的状态看起来像这样:

enum PlayState {
    Playing,
    Paused,
    Error
}

const initialState = {
    playState:  PlayState.Playing,
};

这具有当前选择的音频播放器的状态。使用控制面板,用户可以将玩家的状态从播放变为暂停,反之亦然。 Audio Player React组件会倾听状态更改并调用音频播放器SDK(可能需要几秒钟,并且可能会出错)。

由于这就像一个异步过程,在这种情况下,可以以某种方式应用异步thunk,还是请求,成功,失败模式? 如何正确处理音频播放器的真实状态与预期(请求)状态之间的差异?

Let's say I have a state that looks like this:

enum PlayState {
    Playing,
    Paused,
    Error
}

const initialState = {
    playState:  PlayState.Playing,
};

This holds the state of the currently selected audio player. Using a control panel, user can change the state of the player from playing to paused and vice versa.
Audio player react component listens to state changes and calls the audio player SDK (which can take a few seconds, and can error out potentially).

Since this is like an async procedure, can somehow async thunk be applied in this case, or the request, success, failure pattern?
How to correctly handle discrepency between the TRUE state of the audio player, and the intended (requested) state?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰◇生如夏花灿烂 2025-01-31 06:11:36

是的,也可以使用Thunk,还可以使用Redux-Observable,传奇,悬念,钩子和其他方法来完成。

这是一篇不错的文章,展示了带有Thunk,Saga,悬念和钩子的示例:https://www.freecodecamp.org/news/loading-data-in-react-redux-thunk-redux-saga-suspense-hooks-666b21da1569

我认为,一旦您熟悉上述问题之一,您的问题也会变得轻而易举。 :)

Yes, it can be done using thunk, also using redux-observable, saga, suspense, hooks, and other approaches.

Here is a nice article showing examples with thunk, saga, suspense, and hooks: https://www.freecodecamp.org/news/loading-data-in-react-redux-thunk-redux-saga-suspense-hooks-666b21da1569

I think once you get familiar with even one of the above your problem will be a breeze to solve. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文