在 Redux 存储中存储不可序列化数据项的实际风险是什么?

What are the actual risks of storing non-serializable data items in Redux store?

每当我在 Redux 存储中存储不可序列化的值时,我都会收到以下警告:

在下面的示例中,我在 Redux 状态中存储了一个 Firestore.TimestampDate 对象也会发生这种情况。

A non-serializable value was detected in the state, in the path: blogPost.createdAt. Value: t {seconds: 1583488258, nanoseconds: 805000000}

Take a look at the reducer(s) handling this action type: LOAD_BLOGPOST_SUCCESS. (See https://redux.js.org/faq/organizing-state#can-i-put-functions-promises-or-other-non-serializable-items-in-my-store-state)

这就是文档所说的:

https://redux.js.org/faq/organizing-state#can-i-put-functions-promises-or-other-non-serializable-items-in-my-store-state

Can I put functions, promises, or other non-serializable items in my store state?

It is highly recommended that you only put plain serializable objects, arrays, and primitives into your store. It's technically possible to insert non-serializable items into the store, but doing so can break the ability to persist and rehydrate the contents of a store, as well as interfere with time-travel debugging.

If you are okay with things like persistence and time-travel debugging potentially not working as intended, then you are totally welcome to put non-serializable items into your Redux store. Ultimately, it's your application, and how you implement it is up to you. As with many other things about Redux, just be sure you understand what tradeoffs are involved.

我对上面的文档摘录了解不多。我觉得有点模糊。任何人都可以更详细地解释我们 might/will 通过向 Redux 状态添加不可序列化的数据到底失去了什么吗?

不同数据类型的结果是否不同?例如:一个Promise、一个function (ex: a React component)和一个Date会导致不同的问题吗?它肯定会引起问题吗?或者它可能会发生也可能不会发生?

持久和补充商店内容的能力是什么意思?这是否意味着它可能会破坏我的应用程序代码,或者我们只是在谈论 devtools 调试?


更新

刚刚从 Redux Toolkit 中找到了另一篇文档:Working With Non-Serializable Data

Working with Non-Serializable Data

One of the core usage principles for Redux is that you should not put non-serializable values in state or actions.

However, like most rules, there are exceptions. There may be occasions when you have to deal with actions that need to accept non-serializable data. This should be done very rarely and only if necessary, and these non-serializable payloads shouldn't ever make it into your application state through a reducer.

The serializability dev check middleware will automatically warn anytime it detects non-serializable values in your actions or state. We encourage you to leave this middleware active to help avoid accidentally making mistakes. However, if you do need to turnoff those warnings, you can customize the middleware by configuring it to ignore specific action types, or fields in actions and state:

似乎这部分教你如何忽略操作中使用不可序列化值的警告,但它也说它不应该进入状态,对吗?

坚持和补充商店内容的能力是什么意思?这是否意味着它可能会破坏我的应用程序代码,或者我们只是在谈论 devtools 调试?

可序列化数据,意味着您将数据转换为文本表示,然后将其从文本表示重新加载为实际类型;

坚持和补充商店内容的能力意味着

持久化和重新水化应用程序是一种用于存储当前应用程序商店状态的技术,以便稍后重新加载。 假设您的用户正在处理一些复杂的表单,当他关闭浏览器并重新打开它时,您希望您的表单填写用户在上次会话中输入的内容,但数据不存储在您的后端服务器中。 所以当网页关闭时你坚持你的反应商店,并在用户重新打开网页时重新加载它(从 locastorage 重新水化)

实际发生的情况是,当您想从之前保存的数据中提取应用状态时,non-seriazable 字段将不会被解析并转换为正确的类型(即使在不可序列化的情况下也可能有时会起作用)

因此,如果您不打算持久化和恢复状态,您可以忽略该消息,但您也可以为您的类型实现自定义序列化程序(例如将您的时间戳转换为字符串)