Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I keep the state flat

I'm using ReactJs with Redux and on some tutorials and codes I see people suggesting and using normalizr to keep the state flat. But what is the real advantage in keeping it flat ? Will I encounter any problems if I don't ? Is it necessary ?

like image 596
Mani Shooshtari Avatar asked Aug 09 '16 05:08

Mani Shooshtari


Video Answer


1 Answers

Three main reasons:

  • Updating nested Javascript objects immutably generally results in uglier code that is harder to maintain, unless you use a utility library to wrap up the process
  • Immutably updating nested data requires that you return new copies of all items in the nesting hierarchy. Since components generally do shallow-equality reference comparisons on data to see if they need to update, updating nested data usually means that more objects are updated, and more components will probably have to re-render even if the actual data isn't different.
  • Flat data, and in particular normalized data, enables some more optimized approaches for defining components (such as a list where each list item component is itself connected, given an item ID as a prop, and is responsible for looking up its own item's data by that ID)
like image 170
markerikson Avatar answered Oct 27 '22 01:10

markerikson