What is the purpose of the “@@INIT” action in react-redux?

浪子不回头ぞ 提交于 2021-01-19 19:53:23

问题


Just noticed that it is always the first action dispatched when a page is opened. Is it used to initialize the store with the default state from the reducer?


回答1:


I think this will answer your question.

// 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

UPDATE 24.02.2020

Since @IsaacLyman explicitly asked about this and got some upvotes on the comment, I decided to do an update in order to resolve the issue. To clarify, the source code in question is the following:

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

Source

The "randomString" was first introduced with v4.0.0-beta.1, in the following commit you can see the actual changes (and read a short comment from the author what it is about). Nevertheless, I also found an "official statement" from Dan Abramov here, which states:

"...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."

Therefore, it is actually not intended for the random string to be visible.



来源:https://stackoverflow.com/questions/41305492/what-is-the-purpose-of-the-init-action-in-react-redux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!