"@@ INIT"的目的是什么?在react-redux中采取行动? [英] What is the purpose of the "@@INIT" action in react-redux?

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

问题描述

只需注意,它始终是打开页面时调度的第一个操作.是否使用reducer的默认状态初始化商店?

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 })

来源

更新2020年2月24日

由于@IsaacLyman明确对此进行了询问,并对评论提出了一些反对意见,我决定进行更新以解决此问题.为了澄清,有问题的源代码如下:

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()}`,
  ...
}

来源

"randomString"最初是随 v4.0.0-beta.1 一起引入的,在后续提交中,您可以看到实际的更改(并阅读了作者的意思).然而,我还发现了一个官方声明".来自Dan Abramov 此处,其中指出:

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:

" ...带有 @@ 前缀的任何操作均不打算处理.例如,您永远不要尝试处理 @@ INIT .我们可以通过稍微随机化名称(例如 @@ INIT_2hj3jh34 )来实施该操作.

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

手动处理 @@ INIT 将中断热重装.每次热重载时都会调用它,因此,如果在那里进行初始数据转换,则第二次将不起作用."

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天全站免登陆