What does store do in Redux?
What store is in Redux
The store is an object that:
- holds the entire application state,
- lets you read the current state,
- lets you update it via
dispatch(action),- notifies subscribers (
subscribe) about changes.
You can think of the store as the single source of truth:
the entire application state is kept right there.
How to create a store
javascript
import { createStore } from "redux";
import { counterReducer } from "./counterReducer";
const store = createStore(counterReducer);createStore() takes a reducer: a function that controls
how the state is updated for different actions.
What the store does
| Method | Purpose |
|---|---|
getState() | Returns the current state |
dispatch(action) | Sends an action to the reducer to change the state |
subscribe(listener) | Subscribes a component or function to changes |
replaceReducer(nextReducer) | Swaps the reducer on the fly (rarely used) |
Example
javascript
// 1. Reducer
function counterReducer(state = { count: 0 }, action) {
switch (action.type) {
case "INCREMENT":
return { count: state.count + 1 };
case "DECREMENT":
return { count: state.count - 1 };
default:
return state;
}
}
// 2. Create the store
import { createStore } from "redux";
const store = createStore(counterReducer);
// 3. Subscribe to updates
store.subscribe(() => {
console.log("State changed:", store.getState());
});
// 4. Dispatch actions
store.dispatch({ type: "INCREMENT" });
store.dispatch({ type: "INCREMENT" });
store.dispatch({ type: "DECREMENT" });What happens:
dispatchsends an action;- The store calls
reducer(state, action); - The reducer returns a new state;
- The store saves it and notifies all subscribers;
- Components receive the new state and re-render.
Store in a React application
Usually used together with react-redux:
javascript
import { Provider, useSelector, useDispatch } from "react-redux";
import { store } from "./store";
function Counter() {
const count = useSelector(state => state.count);
const dispatch = useDispatch();
return (
<>
<p>{count}</p>
<button onClick={() => dispatch({ type: "INCREMENT" })}>+</button>
</>
);
}
function App() {
return (
<Provider store={store}>
<Counter />
</Provider>
);
}Here Provider makes the store available to all components,
and the useSelector and useDispatch hooks let you work with it.
Summary
| What the store does | In short |
|---|---|
| Holds the state | The entire application state in one place |
| Manages changes | Updates the state through the reducer |
| Notifies listeners | Notifies on every change |
| Integrates with React | Through <Provider> and the useSelector / useDispatch hooks |
In short:
The
storeis the "single brain" of a Redux application: it knows the entire state, listens for actions, calls reducers, and updates the interface.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.