Predictable state container for JavaScript apps
—
Core store creation and management functionality for maintaining application state. The Redux store serves as the single source of truth for your application state and provides methods to read state, dispatch actions, and subscribe to changes.
Creates a Redux store that holds the state tree.
/**
* Creates a Redux store that holds the state tree.
* @deprecated Use configureStore from @reduxjs/toolkit instead
* @param reducer - Function that returns the next state tree
* @param enhancer - Store enhancer for middleware, time travel, etc.
* @returns Redux store instance
*/
function createStore<S, A extends Action, Ext extends {} = {}, StateExt extends {} = {}>(
reducer: Reducer<S, A>,
enhancer?: StoreEnhancer<Ext, StateExt>
): Store<S, A, UnknownIfNonSpecific<StateExt>> & Ext;
/**
* Creates a Redux store with preloaded state
* @deprecated Use configureStore from @reduxjs/toolkit instead
* @param reducer - Function that returns the next state tree
* @param preloadedState - Initial state
* @param enhancer - Store enhancer for middleware, time travel, etc.
* @returns Redux store instance
*/
function createStore<S, A extends Action, Ext extends {} = {}, StateExt extends {} = {}, PreloadedState = S>(
reducer: Reducer<S, A, PreloadedState>,
preloadedState?: PreloadedState | undefined,
enhancer?: StoreEnhancer<Ext, StateExt>
): Store<S, A, UnknownIfNonSpecific<StateExt>> & Ext;
/**
* Creates a Redux store with flexible parameter handling
* @deprecated Use configureStore from @reduxjs/toolkit instead
* @param reducer - Function that returns the next state tree
* @param preloadedState - Initial state or store enhancer
* @param enhancer - Store enhancer for middleware, time travel, etc.
* @returns Redux store instance
*/
function createStore<S, A extends Action, Ext extends {} = {}, StateExt extends {} = {}, PreloadedState = S>(
reducer: Reducer<S, A, PreloadedState>,
preloadedState?: PreloadedState | StoreEnhancer<Ext, StateExt> | undefined,
enhancer?: StoreEnhancer<Ext, StateExt>
): Store<S, A, UnknownIfNonSpecific<StateExt>> & Ext;Non-deprecated store creation function for use when not migrating to Redux Toolkit.
/**
* Creates a Redux store that holds the state tree (non-deprecated version)
* @param reducer - Function that returns the next state tree
* @param enhancer - Store enhancer for middleware, time travel, etc.
* @returns Redux store instance
*/
function legacy_createStore<S, A extends Action, Ext extends {} = {}, StateExt extends {} = {}>(
reducer: Reducer<S, A>,
enhancer?: StoreEnhancer<Ext, StateExt>
): Store<S, A, UnknownIfNonSpecific<StateExt>> & Ext;
/**
* Creates a Redux store with preloaded state (non-deprecated version)
* @param reducer - Function that returns the next state tree
* @param preloadedState - Initial state
* @param enhancer - Store enhancer for middleware, time travel, etc.
* @returns Redux store instance
*/
function legacy_createStore<S, A extends Action, Ext extends {} = {}, StateExt extends {} = {}, PreloadedState = S>(
reducer: Reducer<S, A, PreloadedState>,
preloadedState?: PreloadedState | undefined,
enhancer?: StoreEnhancer<Ext, StateExt>
): Store<S, A, UnknownIfNonSpecific<StateExt>> & Ext;
/**
* Creates a Redux store with flexible parameter handling (non-deprecated version)
* @param reducer - Function that returns the next state tree
* @param preloadedState - Initial state or store enhancer
* @param enhancer - Store enhancer for middleware, time travel, etc.
* @returns Redux store instance
*/
function legacy_createStore<S, A extends Action, Ext extends {} = {}, StateExt extends {} = {}, PreloadedState = S>(
reducer: Reducer<S, A>,
preloadedState?: PreloadedState | StoreEnhancer<Ext, StateExt> | undefined,
enhancer?: StoreEnhancer<Ext, StateExt>
): Store<S, A, UnknownIfNonSpecific<StateExt>> & Ext;Usage Examples:
import { legacy_createStore as createStore } from "redux";
// Basic store creation
const store = createStore(rootReducer);
// Store with preloaded state
const initialState = { count: 5 };
const store = createStore(rootReducer, initialState);
// Store with enhancer (middleware)
const store = createStore(rootReducer, applyMiddleware(thunk, logger));
// Store with preloaded state and enhancer
const store = createStore(rootReducer, initialState, applyMiddleware(thunk));The Store interface defines the core methods available on Redux store instances.
interface Store<S = any, A extends Action = UnknownAction, StateExt extends unknown = unknown> {
/** Dispatches an action to trigger state changes */
dispatch: Dispatch<A>;
/** Returns the current state tree */
getState(): S & StateExt;
/** Adds a change listener that gets called on every dispatch */
subscribe(listener: ListenerCallback): Unsubscribe;
/** Replaces the reducer currently used by the store */
replaceReducer(nextReducer: Reducer<S, A>): void;
/** Observable/reactive libraries interoperability point */
[Symbol.observable](): Observable<S & StateExt>;
}Dispatches actions to trigger state changes. This is the only way to trigger a state change in Redux.
interface Dispatch<A extends Action = UnknownAction> {
<T extends A>(action: T, ...extraArgs: any[]): T;
}Usage Examples:
// Basic action dispatch
store.dispatch({ type: "INCREMENT" });
// Action with payload
store.dispatch({ type: "ADD_TODO", payload: { text: "Learn Redux" } });
// Using action creator
const increment = () => ({ type: "INCREMENT" });
store.dispatch(increment());Get the current state tree from the store.
/**
* Reads the current state tree managed by the store
* @returns The current state tree of your application
*/
getState(): S & StateExt;Usage Examples:
// Get current state
const currentState = store.getState();
console.log(currentState);
// Use in component
const mapStateToProps = (state) => ({
count: state.counter,
todos: state.todos
});Subscribe to store changes and manage listeners.
/**
* Adds a change listener called on every dispatch
* @param listener - Callback to be invoked on every dispatch
* @returns Function to remove this change listener
*/
subscribe(listener: ListenerCallback): Unsubscribe;
type ListenerCallback = () => void;
interface Unsubscribe {
(): void;
}Usage Examples:
// Subscribe to store changes
const unsubscribe = store.subscribe(() => {
console.log("State changed:", store.getState());
});
// Unsubscribe when no longer needed
unsubscribe();
// Multiple subscribers
const unsubscribe1 = store.subscribe(() => updateUI());
const unsubscribe2 = store.subscribe(() => logChanges());Replace the reducer currently used by the store, useful for code splitting and hot reloading.
/**
* Replaces the reducer currently used by the store to calculate the state
* @param nextReducer - The reducer for the store to use instead
*/
replaceReducer(nextReducer: Reducer<S, A>): void;Usage Examples:
// Hot module replacement
if (module.hot) {
module.hot.accept("./reducers", () => {
const nextRootReducer = require("./reducers").default;
store.replaceReducer(nextRootReducer);
});
}
// Dynamic reducer loading
const loadNewReducer = async () => {
const newReducer = await import("./newReducer");
store.replaceReducer(newReducer.default);
};Redux stores implement the Observable pattern for reactive programming integration.
/**
* Interoperability point for observable/reactive libraries
* @returns A minimal observable of state changes
*/
[Symbol.observable](): Observable<S & StateExt>;
interface Observable<T> {
subscribe: (observer: Observer<T>) => { unsubscribe: Unsubscribe };
[Symbol.observable](): Observable<T>;
}
interface Observer<T> {
next?(value: T): void;
}Usage Examples:
import { from } from "rxjs";
// Convert store to observable
const store$ = from(store);
// Subscribe to state changes with RxJS
store$.subscribe(state => {
console.log("New state:", state);
});type UnknownIfNonSpecific<T> = {} extends T ? unknown : T;
type StoreCreator = {
<S, A extends Action, Ext extends {} = {}, StateExt extends {} = {}>(
reducer: Reducer<S, A>,
enhancer?: StoreEnhancer<Ext, StateExt>
): Store<S, A, UnknownIfNonSpecific<StateExt>> & Ext;
<S, A extends Action, Ext extends {} = {}, StateExt extends {} = {}, PreloadedState = S>(
reducer: Reducer<S, A, PreloadedState>,
preloadedState?: PreloadedState | undefined,
enhancer?: StoreEnhancer<Ext>
): Store<S, A, UnknownIfNonSpecific<StateExt>> & Ext;
};
type StoreEnhancer<Ext extends {} = {}, StateExt extends {} = {}> = <
NextExt extends {},
NextStateExt extends {}
>(
next: StoreEnhancerStoreCreator<NextExt, NextStateExt>
) => StoreEnhancerStoreCreator<NextExt & Ext, NextStateExt & StateExt>;
type StoreEnhancerStoreCreator<Ext extends {} = {}, StateExt extends {} = {}> = <
S,
A extends Action,
PreloadedState
>(
reducer: Reducer<S, A, PreloadedState>,
preloadedState?: PreloadedState | undefined
) => Store<S, A, StateExt> & Ext;Install with Tessl CLI
npx tessl i tessl/npm-redux