Redux

5.0.1 · active · verified Sat Apr 18

Redux is a predictable state container for JavaScript applications. It helps manage application state consistently across different environments and simplifies testing. The current stable version is 5.0.1, which includes bug fixes and type adjustments. Redux maintains an active development cycle with regular patch releases and coordinated major updates, often alongside Redux Toolkit and React-Redux.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic Redux store setup using `createStore`, a simple reducer, and dispatching actions. It shows how to initialize the store, retrieve the current state, and update it via actions.

import { createStore } from 'redux';

interface AppState {
  counter: number;
}

type Action = { type: 'INCREMENT' } | { type: 'DECREMENT' };

const initialState: AppState = { counter: 0 };

function reducer(state: AppState = initialState, action: Action): AppState {
  switch (action.type) {
    case 'INCREMENT':
      return { ...state, counter: state.counter + 1 };
    case 'DECREMENT':
      return { ...state, counter: state.counter - 1 };
    default:
      return state;
  }
}

const store = createStore(reducer);

console.log('Initial state:', store.getState());

store.dispatch({ type: 'INCREMENT' });
console.log('State after increment:', store.getState());

store.dispatch({ type: 'DECREMENT' });
console.log('State after decrement:', store.getState());

// Note: For new applications, it is highly recommended to use Redux Toolkit's `configureStore`
// and `createSlice` for a more efficient and simpler setup.

view raw JSON →