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

标签 javascript serialization redux redux-toolkit redux-devtools

每当我在 Redux 存储中存储不可序列化的值时,都会收到以下警告:
在以下示例中,我存储了 Firestore.Timestamp处于 Redux 状态。 Date 也会发生这种情况目的。

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.


我对上面的文档摘录不太了解。我觉得有点模糊。任何人都可以更详细地解释通过向 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 调试?
可序列化数据,意味着您将数据转换为文本表示,然后将其从文本表示重新加载为实际类型;
持久保存和补充存储内容的能力平均值
持久化和再水化应用程序是一种用于存储当前应用程序商店状态的技术,以便在以后重新加载。
假设您的用户正在处理一些复杂的表单,当他关闭浏览器并重新打开它时,您希望表单填充用户在他上次 session 中输入的内容,但没有将数据存储在后端服务器中。
所以当网页关闭时你会保留你的 react 商店,并在用户重新打开网页时重新加载它(从 locastorage 重新水化)
实际发生的情况是,当您想从之前保存的数据中水合您的应用程序状态时,非可序列化字段将不会被解析并转换为正确的类型(即使不可序列化有时也可以工作)
因此,如果您不打算持久化和重新水化状态,您可以忽略该消息,但您也可以为您的类型实现自定义序列化程序(例如将时间戳转换为字符串)

关于javascript - 在 Redux 存储中存储不可序列化的数据项的实际风险是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64205930/

相关文章:

javascript - 调用 require 时的 const vs let

c# - 使用无效枚举值序列化对象

java - java中如何在两个JVM之间传递对象?

c# - 在没有转换器的情况下自定义 System.Text.Json 的序列化?

javascript - 如何从 redux-observable 调度多个 Action ?

javascript - Angular : Where to put reusable configuration

javascript - 背景附件固定替代方案

javascript - Bootstrap 下拉菜单不起作用

javascript - Object.assign - 更新特定属性

reactjs - 如何清除来自 redux 操作的错误消息