Simple redux store returns undefined

Simple redux store returns undefined
0

#1

I’m trying to learn this library through a minimal build from scratch and can’t figure out why the below returns undefined on the second log statement (first log returns 1 as expected). What am I neglecting here? This is the script in its entirety.

const createStore = require('redux').createStore;

const reducer = (prior_state = { key : 'value' }, action) => {
  switch (action.type) {
    case "1":
      console.log('1');
      break;
  default:
    return prior_state;
  }
};

const store = createStore(reducer);

store.dispatch({
  type: '1',
});

console.log(store.getState());

#2

Your new state will be the returned state from the reducer, that is the reducer job. Since you’re returning nothing in the case "1", the state will be undefined.

const reducer = (state = { key : 'value' }, action) => {
  switch (action.type) {
    case "1":
        return 1; // state is now 1 (and you lost that {key: 'value} you had)
    default:
        return state; // state is now the same state as before
  }
};

#3

Ah ok, that plugs that gap for me :slight_smile: