为什么我的 Redux 存储应该是可序列化的? [英] Why should my Redux store be serializable?

查看:19
本文介绍了为什么我的 Redux 存储应该是可序列化的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读 redux 文档时,我发现了这一点:

When reading the redux docs I found this:

不过,您应该尽最大努力保持状态可序列化.不要在里面放任何你不能轻易变成 JSON 的东西.

Still, you should do your best to keep the state serializable. Don't put anything inside it that you can't easily turn into JSON.

所以我的问题是,保持状态可序列化有什么好处?或者,如果我将不可序列化的数据放入存储中可能会遇到什么困难?

So my question is, what's the benefit of keeping state serializable? Or, what difficulties I may have if I put non-serializable data into store?

而且我相信这不是 redux 独有的 - Flux,甚至 React 本地状态也暗示了同样的事情.

And I believe this is not unique to redux - Flux, even React local state suggest the same thing.

为了让我清楚,这里有一个例子.假设商店结构是这样的.

To make me clear here is an example. Suppose the store structure is like this.

{
    books: { 
        1: { id: 1, name: "Book 1", author_id: 4 }
    },
    authors: {
        4: { id: 4, name: "Author 4" }
    }
}

这看起来应该不错.但是,当我尝试访问第一本书的作者"时,我必须编写如下代码:

This should all looks good. However when I try to access "the author of Book 1", I have to write code like this:

let book = store.getState().books[book_id];
let author = store.getState().authors[book.author_id];

现在,我要定义一个类:

Now, I'm going to define a class:

class Book {
    getAuthor() {
        return store.getState().authors[this.author_id];
    }
}

我的商店将是:

{
    books: {
        1: Book(id=1, name="Book 1")
    },
    ...
}

这样我就可以通过使用:

So that I can get the author easily by using:

let author = store.getState().books[book_id].getAuthor();

第二种方法可以使书"对象知道如何检索作者数据,因此调用者不需要知道书籍和作者之间的关系.那么,为什么我们不使用它,而不是保留普通对象"?在商店里,就像方法 #1 一样?

The 2nd approach could make the "book" object aware of how to retrieve the author data, so the caller does not need to know the relation between books and authors. Then, why we are not using it, instead of keeping "plain object" in the store just as approach #1?

感谢任何想法.

推荐答案

直接来自 redux 常见问题:

我可以在我的商店状态中放置函数、承诺或其他不可序列化的项目吗?

强烈建议您只将普通的可序列化对象、数组和原语放入您的商店.从技术上讲,可以将不可序列化的项目插入存储中,但这样做会破坏存储内容的持久化和再水化能力,并干扰时间旅行调试.

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.

如果您对持久性和时间旅行调试等可能无法按预期工作的事情感到满意,那么完全欢迎您将不可序列化的项目放入您的 Redux 存储中.归根结底,这是您的应用程序,您如何实现它取决于您.与 Redux 的许多其他事情一样,请确保您了解所涉及的权衡.

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 存储应该是可序列化的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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