JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicsreactState Management
Prev
react
advanced
18 min read

State Management

actions
jotai
reducers
redux
redux-toolkit
state-management
useDispatch
useSelector
zustand

Redux provides predictable state with a single store, actions, and reducers (RTK simplifies this with createSlice). Zustand offers a lighter alternative. Choose based on app complexity — most apps can start with useState + Context.

Key Points

1Redux Core Flow

Single store, actions describe events, reducers produce new state, dispatch triggers the flow. Predictable and debuggable.

2Redux Toolkit

createSlice generates actions + reducer together with Immer for ergonomic immutable updates. configureStore sets up sensible defaults.

3useSelector and useDispatch

useSelector subscribes to store slices (re-renders only when selected value changes). useDispatch returns the dispatch function.

4Zustand Alternative

Minimal API, no boilerplate, no Providers — increasingly popular for apps that don't need Redux's full complexity.

5Choosing a Solution

Start with useState + Context. Add Zustand for moderate complexity. Use Redux Toolkit for complex enterprise apps. TanStack Query for server state.

What You'll Learn

  • Explain Redux's core concepts: store, actions, reducers, and dispatch
  • Use Redux Toolkit's createSlice and createAsyncThunk for modern Redux
  • Know when to use Redux vs Zustand vs useState + Context

Deep Dive

As React applications grow, managing state across many components becomes complex. External state management libraries provide centralized stores, predictable update patterns, and tools for debugging.

Redux Core Concepts

Redux implements a unidirectional data flow with four building blocks:

  • Store: A single JavaScript object holding the entire application state (single source of truth)
  • Actions: Plain objects describing what happened: { type: 'todos/add', payload: 'Buy milk' }
  • Reducers: Pure functions that take current state + action and return new state. No side effects, no mutations — always return a new object.
  • Dispatch: The method that sends actions to the store: dispatch({ type: 'todos/add', payload: 'Buy milk' })

The flow: user interaction → dispatch action → reducer produces new state → components re-render.

Redux Toolkit (RTK)

RTK is the official, modern way to write Redux. It eliminates boilerplate:

  • createSlice({ name, initialState, reducers }) — generates action creators and reducer in one declaration. Uses Immer internally so you can write "mutating" logic that produces immutable updates.
  • configureStore({ reducer }) — sets up the store with good defaults (DevTools, thunk middleware)
  • createAsyncThunk('name', async fn) — handles async logic (API calls) with pending/fulfilled/rejected lifecycle actions

React-Redux Hooks

  • useSelector(selector) — subscribes to the store and returns selected state. The component only re-renders when the selected value changes (strict equality === by default). Selectors should be pure functions: const todos = useSelector(state => state.todos).
  • useDispatch() — returns the store's dispatch function for sending actions.

These hooks replaced the older connect() HOC, which wrapped components and mapped state/dispatch to props.

When to Use Redux

Redux adds complexity — don't use it by default. Consider Redux when:

  • Many components at different tree levels need the same state
  • State updates are complex (multiple reducers, middleware logic)
  • You need time-travel debugging (Redux DevTools)
  • The team benefits from Redux's enforced patterns and predictability

For simple apps, useState + useReducer + Context is sufficient.

Modern Alternatives

  • Zustand: Minimal API, no boilerplate, no Providers. const useStore = create(set => ({ count: 0, increment: () => set(state => ({ count: state.count + 1 })) })). Increasingly popular for its simplicity.
  • Jotai: Atomic state management — each atom is independent state. Fine-grained re-renders.
  • TanStack Query (React Query): Not a general state manager — specialized for server state (caching, refetching, synchronization). Often paired with Zustand for client state.

Choosing a Solution

  • Simple app, few shared states → useState + Context
  • Moderate complexity → Zustand or Jotai
  • Complex enterprise app with many reducers → Redux Toolkit
  • Server data (API caching) → TanStack Query + any client state solution

Key Interview Distinction

Redux provides a predictable, debuggable state container with actions + reducers + single store. RTK eliminates Redux boilerplate. useSelector subscribes to specific state slices, useDispatch sends actions. Zustand is the leading lightweight alternative. Most apps should start simple (useState + Context) and add a library only when complexity demands it.

Fun Fact

Redux was created by Dan Abramov and Andrew Clark in 2015, inspired by Elm and Flux. Dan created it as a demo for his React Europe talk on time-travel debugging. He later joined the React team at Facebook, and ironically spent years telling people most apps don't need Redux.

Learn These First

State

beginner

Context API

intermediate

Continue Learning

State

beginner

Context API

intermediate

Hooks

intermediate

Practice What You Learned

How do useSelector and useDispatch work in React-Redux?
mid
state-management
useSelector reads data from the Redux store by accepting a selector function that receives the entire state and returns the needed slice. useDispatch returns the store's dispatch function to send actions. Together they replace the older connect() HOC pattern with a simpler hooks-based API.
When should you use Context API vs Redux for state management?
mid
state-management
Use Context API for simple, low-frequency state (theme, auth, locale) shared across components. Use Redux (or Redux Toolkit) for complex, frequently-updated state that benefits from middleware, selective subscriptions, time-travel debugging, and predictable update patterns.
What is Redux and Redux Thunk, and how do they manage application state and async operations?
senior
state-management
Redux is a predictable state container using a single store with pure reducer functions; Redux Thunk is middleware that enables async logic by allowing action creators to return functions that can dispatch actions and access state.
Previous
State
Prev