Accessing and Changing the Store

Learn how to access and change the store.

We'll cover the following

Accessing the state

The getState() method returns the reference to the current state:

store.getState();
// => { recipes: [], ingredients: [] }

Changing the state

Redux does not allow external changes to the state. Using the state object received from getState() and directly changing values is prohibited. The only way to cause a change of the state is to pass actions to the reducer function, which updates the result used as the new global state.

Sending action objects to reducers is done via the store using the store’s dispatch() method, which accepts a single argument, the action object:

const action = { type: 'ADD_RECIPE', ... }
store.dispatch(action);

Now we can rewrite our reducer to make it able to create an updated state for actions of type 'ADD_RECIPE' and return the current state otherwise:

const reducer = (state, action) => {
  switch (action.type) {
    case 'ADD_RECIPE':
      return Object.assign(...);
    default:
      return state;
    }
  };

Get hands-on with 1200+ tech skills courses.