gpt4 book ai didi

javascript - react-redux 中 "@@INIT" Action 的目的是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:12:12 24 4
gpt4 key购买 nike

刚刚注意到它始终是打开页面时分派(dispatch)的第一个操作。它是否用于使用 reducer 的默认状态初始化存储?

最佳答案

我认为这会回答您的问题。

// When a store is created, an "INIT" action is dispatched so that every
// reducer returns their initial state. This effectively populates
// the initial state tree.

dispatch({ type: ActionTypes.INIT })

Source

2020 年 2 月 24 日更新

因为@IsaacLyman 明确 asked about this并对评论进行了一些投票,我决定进行更新以解决该问题。澄清一下,有问题的源代码如下:

const ActionTypes = {
INIT: `@@redux/INIT${/* #__PURE__ */ randomString()}`,
...
}

Source

“randomString”最初是在 v4.0.0-beta.1 中引入的, 在 following commit你可以看到实际的变化(并阅读作者的简短评论是关于什么的)。尽管如此,我还找到了 Dan Abramov 的“官方声明”here ,其中指出:

"...any actions prefixed with @@ are not meant to be handled. For example, you should never try to handle @@INIT. We might enforce that by slightly randomizing names (e.g. @@INIT_2hj3jh34).

Handling @@INIT manually will break hot reloading. It is invoked at every hot reload, so if you do your initial data transformation there, it won't work the second time."

因此,实际上并不是为了让随机字符串可见。

关于javascript - react-redux 中 "@@INIT" Action 的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41305492/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com