假设我的状态看起来像这样:
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?
发布评论
评论(1)
是的,也可以使用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. :)