Redux & Redux Toolkit Course
Redux & Redux Toolkit
/
Beginner

Store

Definition

The object that brings Actions and Reducers together. The store holds the application state, allows access to state via `getState()`, allows state to be updated via `dispatch(action)`, and registers listeners.

Explain Like I'm New

The giant brain of your app. It's a single JavaScript object that holds all your data. You create it once when your app starts, and you wrap your entire React app inside of it.

Real World Example

The central database of a company. All employees (components) look at this database to get their information.

Common Use Cases

  • •Centralizing application state

Interactive Example

import { createStore } from 'redux';

// A basic reducer
function counterReducer(state = { value: 0 }, action) {
  switch (action.type) {
    case 'counter/incremented':
      return { value: state.value + 1 };
    default:
      return state;
  }
}

// Create the Store! (Legacy Redux way)
let store = createStore(counterReducer);

// You can ask the store what the state is:
console.log(store.getState()); // { value: 0 }

Interview Questions

basic

  • How many Redux Stores should you have in a standard application?

intermediate

  • What method do you use to get the current state out of the Store object?

Flash Cards

Question

How many stores?

Click to reveal answer
Answer

Exactly ONE.

Question

Get current state?

Click to reveal answer
Answer

`store.getState()`.