且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何一个接一个地分派多个动作

更新时间:2021-09-15 08:38:46

是的 redux-thunk 允许你按照你说的做,是的,这是调度多个操作的***实践(我更喜欢一个替代方案,因为这很简单).状态确实会在您调度时(在它返回时)立即更新,这就是为什么给您一个函数来检索 thunk 中的状态,而不是简单地引用状态.通过这种方式,您可以通过一次调度更改状态,然后调用 getState() 以获取对新状态的引用,以便在需要时读取.

Yes redux-thunk allows you to do as you say, and yes it is a best practice for dispatching multiple actions (one that I prefer over the alternatives because of it's simplicity). The state is indeed updated as soon as you dispatch (by the time it returns), which is why you are given a function to retrieve the state in a thunk, instead of simply a reference to the state. In that way you can alter the state with one dispatch, and then call getState() to get a reference to the new state to read from if you need to.