简体   繁体   English

为什么我的 Redux store 应该是可序列化的?

[英]Why should my Redux store be serializable?

When reading the redux docs I found this:在阅读 redux 文档时,我发现了这一点:

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.不要在里面放任何你不能轻易转换成 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?或者,如果我将不可序列化的数据放入存储中,我可能会遇到什么困难?

And I believe this is not unique to redux - Flux, even React local state suggest the same thing.而且我相信这不是 redux 独有的——Flux,甚至 React 本地状态也暗示了同样的事情。


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:但是,当我尝试访问“书 1 的作者”时,我必须编写如下代码:

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];
    }
}

And my store will be:我的商店将是:

{
    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();

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?那么,为什么我们不使用它,而不是像方法 1 一样将“普通对象”保留在商店中?

Any ideas are appreciated.任何想法表示赞赏。

Directly from the redux FAQs :直接来自redux 常见问题解答

Can I put functions, promises, or other non-serializable items in my store state?我可以将函数、承诺或其他不可序列化的项目放入我的 store 状态吗?

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.如果您对持久性和时间旅行调试等可能无法按预期工作的事情感到满意,那么完全欢迎您将不可序列化的项目放入您的 Redux 存储中。 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 的许多其他事情一样,请确保您了解所涉及的权衡。


Further reading:进一步阅读:

Adding to what @Timo said , If you want to setup relation between 2 states in your state tree and use computed values, reselect is the best suitable fit for that scenario.添加到@Timo 所说的内容,如果您想在状态树中设置两个状态之间的关系并使用计算值,那么reselect最适合该场景。 It allows to creare selectors which can be used to define computed states.它允许创建可用于定义计算状态的selectors In your case author can be created using a selector on top of book .在您的情况下,可以使用book顶部的选择器创建author https://github.com/reactjs/reselect https://github.com/reactjs/reselect

@timo 's answer is correct. @timo 的回答是正确的。 In addition, I recommend a library called Redux-ORM to work with normalized/relational data in your Redux store.此外,我推荐一个名为Redux-ORM的库来处理 Redux 存储中的规范化/关系数据。 See my recent comment at Dealing with data consistency in a very large store in React + Redux SPA SaaS for links to more information.有关更多信息的链接,请参阅我最近在 React + Redux SPA SaaS 中处理大型商店中的数据一致性的评论。

Adding this because you asked for a suggestion.添加此内容是因为您要求提供建议。 If you only want to create an object out of your class then you can simply create a function that returns an object.如果您只想从您的类中创建一个对象,那么您可以简单地创建一个返回对象的函数。 In your Book class it could be something like在您的 Book 课程中,它可能类似于

function newBookObject({id, name}) {
    return {id, name}
}

And your store would look like你的商店看起来像

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

The object being returned can't contain any function's in it though, it should be just a plain static object with pure data and nothing else.但是,返回的对象不能包含任何函数,它应该只是一个带有纯数据的普通静态对象,没有别的。 You can't serialize something that isn't pure data.你不能序列化不是纯数据的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么redux中的store应该是常量变量呢? - Why should the store in redux should be a constant variable? 我应该如何在redux存储中存储isFetching / isLoading数据? - How should I store isFetching/isLoading data in my redux store? 我应该在Redux中存储承诺吗? - Should I store promises in Redux? Redux Store 的结构应该是什么? - What should be structure of Redux Store? 为什么我的redux将映射属性存储到具有该值的单个属性的对象? - Why is my redux store mapping properties to an object with a single property with the value? 为什么我的Redux商店没有获得Firebase电子邮件属性? 应对 - Why my redux store is not getting firebase email property? React 如果我的 Redux 商店中的 state 是可观察的,我应该在每个需要它的组件中订阅它还是在一个地方订阅并作为输入传递? - If a state in my Redux Store is an observable, should I subscribe to it in each component that needs it or subscribe in one place and pass as inputs? 为什么 Redux 中的对象应该是不可变的? - Why should objects in Redux be immutable? React-Redux:是否应该将所有组件状态保存在 Redux Store 中 - React-Redux: Should all component states be kept in Redux Store 在我的 redux 商店中将我的插座放在哪里? - Where to hold my socket in my redux store?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM