title: How to handle my redux store with Lodash and Normalizr?
body: <p>I just don't get it. This is all alchemy and the promises of making frontend development easier obscure the various ingredients that you have to mix just right for those promises to hold true. Either I'm mixing the wrong things together, or my chants are off tune. I don't know.</p>
  
  <p>This is going to be a mix of questions. Maybe typing it all out will help me figure out where I seem to be getting this all wrong. </p>
  
  <p>I'm using normalizr to flatten out my API response. It took me a while to even figure out I should use something like that. Fighting arrays of objects in redux was fun for a while, but now I'm on the flattening responses stage of the journey. So my API response returns a list of people and normalizr breaks it down into the store like:</p>
  
  <pre><code>people: { 
    people: { 
      entities: {
        1: { id: 1, name: "jim" },
        2: { id: 2, name: "billy" }
      },
      results : [ 1 , 2 ]
    }
  } 
  </code></pre>
  
  <p>Why is people two keys deep? I don't know. I couldn't get the reducer to work any other way. I'm going by examples I saw people using. Here's my reducer (simplified to show the relevant piece):</p>
  
  <pre><code>case "GET_PEOPLE": {
    return  {...state, people: action.payload}
  }
  </code></pre>
  
  <p>Okay well that works fine. great even! Until I want to add more people from the API. See, for some reason if I drop more people into this reducer it just replaces everything instead of adding them. Okay so I'm doing this wrong. A little bit more research brings up this thing called Lodash which has a merge function. Perfect! I'm not merging the results in the store, I must be replacing them (or that's what it seems). So let's use Lodash in the reducer to merge results instead of replace them.</p>
  
  <pre><code>case "SET_PEOPLE": {
    return  merge({}, state, action.payload)
  }
  </code></pre>
  
  <p>Alright so it looks fine, and it works! Results are merged instead of replaced. And this way has the added benefit of not needing to add that extra people key in the reducer. So I actually went and changed a lot of my code to not use that extra people key. Until I find out Lodash is duplicating keys in the result array.</p>
  
  <p>A lot of this stuff seems really new. It seems like people are still figuring out the "right" way to do things, and what tools are most effective. At least that what it seems like from scouring various guides, READMEs, and documentation. They all tell you to use a different approach (and they've only been written months apart at most).</p>
  
  <p>I think pentagrams, candles, and chanting would be as effective at this point, but I'm holding out hope someone can help me with the right ingredients here. I'm using React, which led to needing Redux, which led to needing Normalizr, which apparently leads to needing Lodash. Is that correct? Am I on the right path there? If not, what are the minimum set of tools to make frontend state management a bearable process. Conceptually I love what I'm working with but there's so many gotchas it's ridiculous. </p>
  
  <p>The reducer:</p>
  
  <pre><code>export default function reducer(state={
      people: {
          entities : {
            people: {
              0 : {
                name : ''
              }
            }
          },
          result : [0,]
      }
    }, action) {
    switch (action.type) {
      case "GET_PEOPLE": {
        const newpPeople = {...state.people, ...action.payload };
        console.log(state.people)
        console.log(action.payload)
        console.log(newpPeople)
        return {...state, people : newpPeople};
      }
      default :
        return state
    }
  }
  </code></pre>
  
  <p>This first console log is the state after the reducer has been used once. it has the initial set of people that i've state to the store:</p>
  
  <pre><code>{ 
      entities: { 
        people : { 
          1 : { id: 1, name: "jim" },
          2 : { id: 2, name: "billy" }
        }
      },
      result : [ 1, 2 ]
  }
  </code></pre>
  
  <p>The second console log will be the payload of new people to be added:</p>
  
  <pre><code>{ 
      entities: { 
        people : { 
          7 : { id: 7, name: "sally" },
          8 : { id: 8, name: "ana" }
        }
      },
      result : [ 7, 8 ]
  }
  </code></pre>
  
  <p>Then the third console log should be the two states combined? But it just repeats the last one with sally and ana, and overwrites everything else.</p>