question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Documentation request: Working with normalized data in React+Redux, "denormalizing" data best practices

See original GitHub issue

The documentation for redux and redux+react is really really great but I am really missing one thing that has confused me for a while.

When working with a flat/normalized state shape it’s a bit unclear for me when the best time is to “denormalize” the data, that is lookup the real data/entities so I can pass it to my component or it’s child components.

Let’s make an example, we have built a book recommendation app/webpage.

const state = {

    bookLists: {
        0: {
            title: "Great fantasy books",
            books: [0, 1]
        },
        1: {
            title: "Best Sci-Fi of the year",
            books: [2]
        }
    }
    books: {
        0: {
            author: "J.R.R Tolkien",
            title: "Lord of the rings"
        },
        // And so on...
    }
}

Now let’s imagine we want to render all book lists and their corresponding books on the same page. Our component hierarchy would probably be something like:

<BookPage>
    <BookList>
        <Book />
        <Book />
        <Book />
   </BookList>
   <BookList>
        <Book />
        <Book />
        <Book />
    </BookList>
    ...
</BookPage>

Questions + answers I wish we could update the documentation with (or discuss here):

Each book needs to be resolved from the ids of the booklist. Where would this be done? Options I can think of:

  • In the mapStateToProps for the BookList by doing a .map for each book id?
  • By passing the book ids to the child Book components and have them use connect to get the information from the state?
  • Another better option?

Each of the suggested options have some problems:

  • In the first option (mapStateToProps and .map from ids) each BookList will be re-rendered when the state is changed since the mapping between id and book objects would create a new array of books which when passed to a BookList would cause a shallow comparion to fail causing a re-render.

  • Using the second option instead (passing ids to child components) shallow comparison would work better but feels “wrong”. Should a component really receive only an id? It feels hard to re-use and test them that way?

How would you handle this? I would love some guidance!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:13
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
OliverJAshcommented, Feb 28, 2018

@johot

In the first option (mapStateToProps and .map from ids) each BookList will be re-rendered when the state is changed since the mapping between id and book objects would create a new array of books which when passed to a BookList would cause a shallow comparion to fail causing a re-render.

In that scenario, wouldn’t the book objects inside the new books array have the same reference? In that case, BookList would pass a shallow equal comparison for the books array prop? Furthermore, Book would receive the same reference book objects as props, so that too would pass a shallow equal comparison for the book object prop.

0reactions
markeriksoncommented, Nov 15, 2017

@bonusrk : anything with a “documentation” tag in either repo is available to be worked on.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Normalizing State Shape | Redux
Structuring Reducers > Normalizing State Shape: Why and how to store data items for lookup based on ID.
Read more >
What is denormalization and how does it work? - TechTarget
Normalizing a database involves removing redundancy so only a single copy exists of each piece of information. Denormalizing a database requires data has...
Read more >
Is it better to send normalized or denormalized API response ...
At the beginning it calls a single endpoint, which returns a good amount of data as a heavily nested JSON. I then normalize...
Read more >
When and How You Should Denormalize a Relational Database
However, retrieving data from a normalized database can be slower, ... repeatedly during queries, it's best to store the results of it.
Read more >
Two mistakes I made working with Redux - Matt Zeunert
1) Using normalized data throughout the component tree. With Redux your store will generally contain normalized data. Your data might look ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found