javascript - Redux - Why normalize? -


i have been trying learn how better structure redux stores , stumbled upon lesson dan.

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

although understand how go normalizing data in way, not understand motivation behind it. particularly, have 2 questions.

  1. why wont simple arrays sufficient? dan mentions - "in complex apps, might have more single array , todos same ids in different arrays might out of sync". did not understand this, may have example? benefit see using object improved efficiency not need map on entire array, in case want delegate todo reducer.

  2. why need maintain list of allids? why maintain additional state, when can map on list of todos , obtain it?

i know normalizr us, why should normalize in first place? make sense normalize if responses not nested?

edit 1:

thanks answer, christopher.

let assume state tree looks 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: []     }    } 

i not see benefit of having object in place of array here. have selectors fetch subscribed todo id departments, if array. seems understanding of concept bit deficient. please elaborate?

  1. "in complex apps, might have more single array , todos same ids in different arrays might out of sync."

you may have todo (say id 1) in "user's todos" list , "department's todos" list @ same time, example. , if user updates todo, todo should updated in "department's todos" list. if data normalized, todo updated in both places (well, there 1 instance of todo referred multiple places). if it's not normalized, department have stale copy of todo.

  1. "why keep list of ids?"

i think you're right, honestly. if you're going bother normalizing, duplicating list of ids kind of seems run counter effort.

anyway, think case normalizing data in react mirrors case normalizing data in general (e.g. in databases). it's bit more effort front, flexibility gives worth it.

also, don't normalize react code, find not normalizing ends making code sloppier on time. become less disciplined. guess it's broken-window effect. in non-normalized code, start throwing values places shouldn't out of convenience.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -