React & Modern Frontend (445 Qs)•Part 10: Modern React 18, React 19 & Next.js Patterns (317 - 353)
What happens when you dispatch an action?
store.dispatch(action)is called with a plain action object ({ type, payload }).- The action passes through the middleware chain in order (e.g., logger → thunk → your custom middleware); async/thunk middleware may intercept it here (e.g., a thunk function is invoked instead of being forwarded, or a Promise/generator is handled) or asynchronous side-effects can trigger further dispatches later.
- Once every middleware calls
next(action), the action reaches the root reducer. - The root reducer (usually built with
combineReducersor RTK'sconfigureStore) forwards the action to every slice reducer, each returning either its unchanged state or a new state object. - The store replaces its internal state tree with the combined result and marks the update complete.
- The store notifies all subscribers (React-Redux's
Provider/useSelectorinternals) that state changed. - Each connected component's selector is re-run and compared (via
===or a custom equality function) against its previous result; components only re-render if their selected slice actually changed.
Practical Code Example
text
// Action Dispatch Cycle:
// dispatch(action) -> Middlewares intercept -> Reducer calculates nextState ->
// Store updates -> Subscribed selectors notify components -> Components re-render