Sub topic 2
Creating the Store with `configureStore`
This chapter assumes you know React function components, hooks, ES modules, and basic immutability. It focuses on how a Redux Toolkit store connects to React and how to shape and type its state.
Creating the store with `configureStore`
A Redux store holds shared application state. configureStore creates the store, combines slice reducers, enables useful middleware, and configures Redux DevTools.
import { configureStore, createSlice } from '@reduxjs/toolkit'
const counterSlice = createSlice({
name: 'counter',
initialState: { value: 0 },
reducers: {
incremented(state) {
state.value += 1
},
},
})
export const { incremented } = counterSlice.actions
export const store = configureStore({
reducer: {
counter: counterSlice.reducer,
},
})The counter key becomes the state path state.counter. Each slice should usually own one coherent feature rather than one unrelated field.
Connecting React
Provider makes the store available to descendant components. useSelector reads state and subscribes the component to changes. useDispatch sends an action to the store.
import { Provider, useDispatch, useSelector } from 'react-redux'
import { store, incremented } from './store'
function Counter() {
const value = useSelector((state) => state.counter.value)
const dispatch = useDispatch()
return (
<button onClick={() => dispatch(incremented())}>
Count: {value}
</button>
)
}
export default function App() {
return (
<Provider store={store}>
<Counter />
</Provider>
)
}Redux follows one-way data flow: a component dispatches an action, a reducer calculates the next state, and subscribed components render the new value.
Deriving data with selectors
A selector is a function that reads or derives state. Use createSelector when derivation is expensive or returns a new array or object. It memoizes the result until an input changes.
import { createSelector } from '@reduxjs/toolkit'
const selectItems = (state) => state.todos.items
const selectFilter = (state) => state.todos.filter
export const selectVisibleItems = createSelector(
[selectItems, selectFilter],
(items, filter) =>
filter === 'done' ? items.filter((item) => item.completed) : items
)Choosing and shaping state
Keep shared client state in Redux when multiple parts of the application need it or when its updates need centralized debugging. Keep temporary form input and component-only UI state local. Prefer normalized collections for large, frequently updated entity sets. Do not copy values that can be derived with a selector.
Typing the store and hooks
Derive types from the store so they stay synchronized with the reducer configuration.
export type RootState = ReturnType<typeof store.getState>
export type AppDispatch = typeof store.dispatch
export const useAppSelector = useSelector.withTypes<RootState>()
export const useAppDispatch = useDispatch.withTypes<AppDispatch>()Use these hooks in components to get checked state paths, action payloads, and dispatch calls.
Exercise
Where should a component-local modal-open flag usually live?
Show answer
In the component with useState, unless unrelated parts of the application must coordinate that flag.