Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 657
Marcelo Avatar asked Dec 23 '16 17:12

Marcelo


Video Answer


1 Answers

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.

like image 180
Ramiz Wachtler Avatar answered Oct 13 '22 09:10

Ramiz Wachtler