Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Ngrx / Redux Gotcha: Don't Forget To Return "State" In The Reducer Function

12/25/2016

0 Comments

 
Here's an error I was having for the past few days that I'm a little embarrassed to admit. haha. As it t urns out the error was caused by me not returning anthing from a certain case of the swtich statement in my reducer function. 

The Problem

The problem all began when I started adding new actions to my project. I initially just added the actions to the switch statement inside of my reducer function, but out of habit I just ended the case statement with a "break". For a case inside of a reducer function though we don't want to just end the loop and return nothing because remember, the output of your reducer is the thing that will be the new state. If you just end with a break and return undefined... then the entire state becomes undefined! And that's exactly what was happening to me. Here's an example of some bad code:
export const mainStoreReducer: ActionReducer<State> =
  (state = intitialState, action: Action) => {
    switch (action.type) {

      case ActionTypes.AUTH_SUCCESS: {
        // TODO Implement reducer function for this action.

         break;
      }
  }
And here's the error it was causing in my debug console:
zone.js:388Unhandled Promise rejection: Error in ./MainComponent class MainComponent - inline template:2:4 caused by: Cannot read property 'userData' of undefined ; Zone: angular ; Task: Promise.then ; Value: ViewWrappedError {_nativeError: Error: Error in ./MainComponent class MainComponent - inline template:2:4 caused by: Cannot read pro…, originalError: TypeError: Cannot read property 'userData' of undefined
    at MapSubscriber.project (http://localho…, context: DebugContext}
The important bit in the output above is the part that says, "Cannot read property 'userData' of undefined". It was telling me that this error was occurring here where I was selecting this 'userData' property: 
this.store.select(s => s.mainStoreReducer.userData)
  .subscribe((data: any) => {
  console.log('data is' + data)
});

The Solution

The solution, of course, it to always return a state object (and because this is TypeScript we can say we must return some object that implements the interface defined by your state). Since the current state is passed in as an agument to the reducer function we could always just return that if we don't want our reducer to do anything:
case ActionTypes.AUTH_SUCCESS: {

   // TODO Implement reducer function for this action.
   
   return state;
}
Once you start implementing the logic for this action you'll probably want to make a clone of the current state, modify some property on the new clone, and then return the clone to be set as the new state. Well the Object.assign() method does just that (but be sure not to forget the empty object in the arguments). 
return Object.assign({}, state,  {
    userData: {
    counter:action.payload.counter + 1}
  }
)

The Lesson

The life lession here for you to take home and tell to your grandkids is that inside of a Redux reducer function you HAVE to return something that implements your state. Do not use a break statement. Return a modified copy of the state. Sleep tight new york.✌
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Want FREE access to
    my daily stock tips 
    ​newsletters??
    Sign up here:

    - Triple Gainers
    - Rippers N' Dippers
    - Growingest Growers
    ​

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021