Configure a Redux Toolkit store
A store is shared application memory
Redux manages application state through a store. A store holds the current state, accepts actions and notifies subscribers after dispatch. A reducer calculates the next state from the previous state and an action. Redux is the core state library; Redux Toolkit (RTK) provides convenient store and reducer APIs; React Redux connects React components to the store. Start with local React state for a text field or an open menu. Reach for shared state when multiple distant parts need a consistent workflow. This chapter uses RTK 2.x and React Redux 9.x conventions (typed Hooks require 9.1 or later), with a reviewed React/Vite project and exact package lock still pending. Each JSX/TSX block is an independent App file; the examples use no network.
import React, { useState } from 'react';import { configureStore, createSlice } from '@reduxjs/toolkit';import { Provider, useSelector, useDispatch } from 'react-redux';function counter(state = {count:0}, action) { if (action.type === 'counter/increment') return {count:state.count+1}; return state;}function makeStore(){return configureStore({reducer:{counter},devTools:false});}function Panel(){const count=useSelector(state=>state.counter.count);return <p>Count: {count}</p>;}export default function App(){const [store]=useState(makeStore);return <Provider store={store}><Panel /></Provider>;}The first screen reads Count: 0. Passing a reducer map to configureStore creates a root state with a counter property. The Provider passes the store through React context; useSelector reads the count and subscribes to relevant changes. The reducer handles initialization and returns existing state for unrelated actions. configureStore also provides default middleware and optional DevTools integration. Here DevTools are disabled for the isolated fixture. The store is created once for each mounted App instance using a pure lazy initializer, not on every render. Development StrictMode can call initializers more than once; no external effects belong there.
Send an action, not an assignment
import React, { useState } from 'react';import { configureStore, createSlice } from '@reduxjs/toolkit';import { Provider, useSelector, useDispatch } from 'react-redux';function counter(state = {count:0}, action) { if (action.type === 'counter/increment') return {count:state.count+1}; return state;}function makeStore(){return configureStore({reducer:{counter},devTools:false});}function Panel(){const count=useSelector(s=>s.counter.count);const dispatch=useDispatch();return <section><p>Count: {count}</p><button onClick={()=>dispatch({type:'counter/increment'})}>Increment</button></section>;}export default function App(){const [store]=useState(makeStore);return <Provider store={store}><Panel /></Provider>;}After one click the reducer returns a new counter with count 1. Dispatch of this plain action updates the Redux store synchronously under the standard middleware; React schedules its own rendering separately. The handler should not assign to the selected count or mutate an object taken from the store. The action type names what happened; it is not a function name that Redux calls automatically. A reducer must be deterministic and free of side effects.
Keep stores isolated and state ownership deliberate
import React, { useState } from 'react';import { configureStore, createSlice } from '@reduxjs/toolkit';import { Provider, useSelector, useDispatch } from 'react-redux';function counter(state = {count:0}, action) { if (action.type === 'counter/increment') return {count:state.count+1}; return state;}function makeStore(){return configureStore({reducer:{counter},devTools:false});}function Counter(){const count=useSelector(s=>s.counter.count);const dispatch=useDispatch();return <button onClick={()=>dispatch({type:'counter/increment'})}>Count: {count}</button>;}function Island({title}){const [store]=useState(makeStore);return <section><h2>{title}</h2><Provider store={store}><Counter /></Provider></section>;}export default function App(){return <><Island title="Left"/><Island title="Right"/></>;}Clicking the left button should change only the left count. Independent stores are useful for isolated tests and separately mounted widgets; most applications use one store for shared application state. For SSR, create a store per request rather than sharing a mutable singleton. Keep transient input drafts local unless another feature actually needs them. Store plain data, not DOM elements or secrets; development checks are diagnostic, not authorization or runtime validation.
Practice
Trace initialization, two increments and an unrelated action in the first reducer. Sketch which store each button uses in the two-panel example.