“@@INIT"的目的是什么?react-redux 中的操作? [英] What is the purpose of the "@@INIT" action in react-redux?

查看:25
本文介绍了“@@INIT"的目的是什么?react-redux 中的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚注意到它总是在打开页面时调度的第一个动作.是不是用reducer的默认状态来初始化store?

解决方案

我想这会回答你的问题.

<块引用>

//当一个存储被创建时,一个INIT";动作被分派,以便每个//reducer 返回它们的初始状态.这有效地填充//初始状态树.dispatch({ type: ActionTypes.INIT })

来源

更新 24.02.2020

由于@IsaacLyman 明确询问了这个问题 并在评论中获得了一些赞成票,我决定进行更新以解决该问题.澄清一下,有问题的源代码如下:

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

来源

随机字符串"首次引入 v4.0.0-beta.1,在 以下提交中,您可以看到实际的更改(并阅读来自作者是关于什么的).尽管如此,我也发现了一篇官方声明"来自 Dan Abramov 此处,其中指出:

<块引用>

"...任何以 @@ 为前缀的操作都不会被处理.例如,您永远不应该尝试处理 @@INIT.我们可能会通过稍微随机化名称来强制执行此操作(例如 @@INIT_2hj3jh34).

手动处理 @@INIT 会破坏热重载.它会在每次热重载时调用,因此如果您在那里进行初始数据转换,第二次将无法工作."

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

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?

解决方案

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.

这篇关于“@@INIT"的目的是什么?react-redux 中的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆