JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

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

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsreact
PrevNext

Learn the concept

State Management

react
senior
state-management

What is Redux and Redux Thunk, and how do they manage application state and async operations?

redux
state-management
thunk
middleware
async
actions
reducers
redux-toolkit
Quick Answer

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.

Detailed Explanation

Redux Core Concepts:

  1. Store - Single source of truth holding entire app state
  2. Actions - Plain objects describing what happened { type: 'ADD_TODO', payload }
  3. Reducers - Pure functions: (state, action) => newState
  4. Dispatch - Method to send actions to store

Unidirectional Data Flow:

UI → dispatch(action) → reducer → new state → UI re-renders

What is a Thunk?

  • A function that wraps an expression to delay its evaluation
  • In Redux: functions that can access dispatch and getState
  • Enables async operations and conditional dispatching

Why Redux Thunk?

  • Reducers must be pure (no side effects)
  • Actions must be plain objects
  • Thunks allow async code in action creators

Modern Redux Toolkit:

  • configureStore - Sets up store with good defaults
  • createSlice - Generates actions + reducers together
  • createAsyncThunk - Handles async with pending/fulfilled/rejected
  • RTK Query - Built-in data fetching and caching

Code Examples

Redux fundamentals (legacy pattern)JavaScript
// Action types
const ADD_TODO = 'todos/add';
const TOGGLE_TODO = 'todos/toggle';

// Action creators
const addTodo = (text) => ({
  type: ADD_TODO,
  payload: { id: Date.now(), text, completed: false }
});

const toggleTodo = (id) => ({
  type: TOGGLE_TODO,
  payload: id
});

// Reducer (pure function!)
const initialState = { todos: [], loading: false, error: null };

function todosReducer(state = initialState, action) {
  switch (action.type) {
    case ADD_TODO:
      return {
        ...state,
        todos: [...state.todos, action.payload]
      };
    case TOGGLE_TODO:
      return {
        ...state,
        todos: state.todos.map(todo =>
          todo.id === action.payload
            ? { ...todo, completed: !todo.completed }
            : todo
        )
      };
    default:
      return state;
  }
}

// Store setup
import { createStore } from 'redux';
const store = createStore(todosReducer);

// Usage
store.dispatch(addTodo('Learn Redux'));
store.dispatch(toggleTodo(1));
console.log(store.getState());

Real-World Applications

Use Cases

Enterprise State Architecture

Structuring Redux stores for large teams with feature-based slice organization

Offline-First Applications

Using Redux for client-side state that syncs with server when connectivity returns

Complex Async Workflows

Managing multi-step async operations (API calls, retries, rollbacks) with Redux Thunk/RTK Query

Mini Projects

Redux Toolkit Migration

intermediate

Migrate a legacy Redux app (createStore, switch reducers, redux-thunk) to modern RTK (configureStore, createSlice, createAsyncThunk)

State Management Comparison

advanced

Build the same feature (shopping cart with server sync) using Redux Toolkit, Zustand, and TanStack Query to compare approaches

Industry Examples

WordPress Gutenberg

Uses multiple Redux stores for managing block editor state

Spotify Web Player

Uses Redux for managing playback state and user library

Resources

Redux Docs - Writing Logic with Thunks

docs

Redux Toolkit Documentation

docs

Redux Essentials Tutorial

article

Related Questions

What is React Context and when should you use it?

mid
context

Explain the useState and useEffect hooks and their common patterns.

mid
hooks
Previous
What are Higher-Order Components (HOCs) in React, and why are they less commonly used today?
Next
What security vulnerabilities should you address in React applications and how do you prevent them?
PrevNext