Why is an array of all `ids` needed in a normalized state shape?
Asked Answered
U

1

9
comments : {
    byId : {
        "comment1" : {
            id : "comment1",
            author : "user2",
            comment : ".....",
        },
        "comment2" : {
            id : "comment2",
            author : "user3",
            comment : ".....",
        },
        "comment3" : {
            id : "comment3",
            author : "user3",
            comment : ".....",
        },
        "comment4" : {
            id : "comment4",
            author : "user1",
            comment : ".....",
        },
        "comment5" : {
            id : "comment5",
            author : "user3",
            comment : ".....",
        },
    },
    allIds : ["comment1", "comment2", "comment3", "commment4", "comment5"]
}

In the above example, is there any reason my needs to include it api include it. I assume this way you can do a count faster, you can probably sort but generally I am not understanding if there is a performance hit.

Undergo answered 8/9, 2017 at 21:50 Comment(3)
Where does that requirement come from? It's certainly not a JavaScript or React thing. I can't speak for Redux.Burny
This isn't a requirement from Redux either.Joelynn
I've seen it in several questions. Apparently it's how Dan Abramov does it in this tutorial: egghead.io/lessons/javascript-redux-normalizing-the-state-shapeNydianye
I
8

This isn't anything that's required by Redux, this is a normalizr thing. To answer your question, JavaScript objects can't be replied upon to retain sort order in certain situations. Putting the ids in an array allows you to retain the sort order that was present before you normalized.

Quote from co-maintainer of Redux and author of "normalizing state shape section" of Redux docs:

As for the ID arrays, while JS engines now have a fairly standardized process for iterating across keys in an object, you shouldn't rely on that to define ordering. Storing arrays of IDs allows you to define an order for items.

Ingemar answered 8/9, 2017 at 22:48 Comment(3)
"To answer your question, JavaScript objects don't guarantee the order in which properties appear." Yes, they do, as of ES2015, see here and here and Oriol's explanation here. New operations added in ES2015, and JSON.stringify, follow the order. Legacy operations (Object.keys, for-in) are not required to. But JSON does not have property order. :-)Burny
normalizr was created in 2014, React apps can be used in as old browsers as IE 9, normalizr does not return JSON so JSON's lack of property order is not relevant here, the specs don't require respecting sort order when enumerating over your data (which is extremely common in React apps), and the author of the library explicitly states unreliable sort ordering as his reasoning here I'll change to "can't be relied upon to retain"Ingemar
correction: Mark wrote the Normalizing State Shape doc for Redux and is a co-maintainer of Redux with its creator, Dan Abramov, who also created Normalizr.Ingemar

© 2022 - 2024 — McMap. All rights reserved.