Redux - 为什么要正常化? [英] Redux - Why normalize?

查看:140
本文介绍了Redux - 为什么要正常化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力学习如何更好地构建我的Redux商店,并偶然发现Dan的这一课。

I have been trying to learn how to better structure my Redux stores and stumbled upon this lesson by Dan.

https://egghead.io/lessons/javascript-redux-normalizing-the-state-shape#/guidelinesModal

虽然我理解如何以这种方式规范化我的数据,但我不明白它背后的动机。特别是,我有两个问题。

Although I understand how to go about normalizing my data in this way, I do not understand the motivation behind it. Particularly, I have two questions.


  1. 为什么简单数组不够用? Dan提到 - 在复杂的应用程序中,我们可能有不止一个阵列,并且在不同阵列中具有相同ID的待机可能会失去同步。我不明白这个,我可以举一个例子吗?我从使用对象中看到的唯一好处是提高了效率,因为我们不需要映射整个数组,以防我想将某个待办事项委托给另一个reducer。

  1. Why wont simple arrays be sufficient? Dan mentions - "In complex apps, we might have more than a single array and todos with the same IDs in different arrays might get out of sync". I did not understand this, may I have an example? The only benefit I see from using an object is improved efficiency as we do not need to map over the entire array, in case I want to delegate a certain todo to another reducer.

为什么我们需要维护allIds列表?当我们可以轻松地映射所有待办事项列表并获取它时,为什么要保持这种额外状态呢?

Why do we need to maintain a list of allIds? Why maintain this additional state, when we can easily map over the list of all todos and obtain it?

我知道normalizr为我们做了这个,但为什么我们首先要正常化呢?即使响应没有深度嵌套,标准化也有意义吗?

I know that normalizr does this for us, but why should we normalize in the first place? Does it make sense to normalize even if the responses are not deeply nested?

编辑1:

感谢您的回答,Christopher。

Thanks for your answer, Christopher.

我们假设您的州树看起来像这样

Let us assume that your state tree looks like this

{
    user: {
        byId: {
            1: {
                id: 1,
                name: 'Buy stuff'
               }, 
            2: {
                id: 2,
                name: 'Yeah'
               }
            }
        },
        allIds: [1, 2],
        subscribedIds: [5],
    }
    department: {
        byId: {
            5: {
                id: 5,
                name: 'Sell Stuff'
            }
        },
        allIds: [5],
        subscribedIds: []
    }   
}

我没有看到在这里有一个对象代替数组的好处。我也可以选择能够从部门获取ID订阅的待办事项,即使它是一个数组。看来我对这个概念的理解有点不足。你能详细说明一下吗?

I do not see the benefit of having an object in place of an array here. I could as well have selectors which would fetch the subscribed todo by ID from departments, even if it is an array. It seems my understanding of the concept is a bit deficient. Could you please elaborate?

推荐答案


  1. 在复杂的应用程序中,我们可能只有一个数组并且在不同阵列中具有相同ID的待办事项可能会不同步。

您可能有一个TODO(比如id为1)例如,它位于用户的TODO列表和部门的TODO列表中。然后,如果用户更新她的待办事项,那么TODO也应该在部门的TODO列表中更新。如果您的数据被标准化,TODO将在两个地方更新(嗯,实际上只有一个TODO实例,它只是从多个地方引用)。但如果它没有正常化,该部门将有一个陈旧的待办事项副本。

You may have a TODO (say with id 1) which is in a "User's TODOs" list and a "Department's TODOs" list at the same time, for example. And then if a user updates her todo, that TODO should also be updated in the "Department's TODOs" list. If your data is normalized, the TODO will be updated in both places (well, there will really only be one instance of the TODO which is simply referred to from multiple places). But if it's not normalized, the department will have a stale copy of the todo.


  1. 为什么要保留一份清单所有ids?

我认为你是对的,说实话。如果你打算进行规范化,那么重复ID列表似乎与这种努力背道而驰。

I think you're right, honestly. If you're going to bother normalizing, then duplicating a list of IDs kind of seems to run counter to that effort.

无论如何,我认为规范化数据的情况如下: React可能反映了一般规范化数据的情况(例如在数据库中)。预先付出更多的努力,但它给你的灵活性通常是值得的。

Anyway, I think the case for normalizing data in React probably mirrors the case for normalizing data in general (e.g. in databases). It's a bit more effort up front, but the flexibility it gives you is generally worth it.

另外,我并不总是规范化我的React代码,但我发现规范化最终导致我的代码随着时间推移变得更加笨拙。我变得不那么自律了。我想这就像破窗效应。在非规范化代码中,我只是开始将值抛出到非常不方便的地方。

Also, I don't always normalize my React code, but I find that not normalizing ends up making my code sloppier over time. I just become less disciplined. I guess it's like the broken-window effect. In non-normalized code, I just start throwing values into places they really probably shouldn't be simply out of convenience.

这篇关于Redux - 为什么要正常化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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