wasAsked.

What is Redux and when should you use it in a React application?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · React, JavaScript

Community answers

Posted by wasAsked editorial collection

Redux is a predictable state management library for JavaScript applications, most commonly used with React. It keeps global application state in a single store and updates it through a strict, one-way data flow. Today the recommended way to use Redux is Redux Toolkit (RTK), which removes most of the old boilerplate. Core concepts - Store: a single object holding the application's global state. - Actions: plain objects describing what happened ({ type: 'cart/itemAdded', payload: item }). - Reducers: pure functions (state, action) => newState that compute the next state without side effects. - Dispatch: sending an action to the store. - Selectors: functions that read specific pieces of state. - Middleware: handles side effects such as API calls (thunks) and logging. Data flow: the UI dispatches an action, reducers compute new state, the store updates, and components subscribed to that state re-render. Redux Toolkit example import { createSlice, configureStore, PayloadAction } from '@reduxjs/toolkit'; type CartItem = { id: number; name: string; qty: number }; const cartSlice = createSlice({ name: 'cart', initialState: [] as CartItem[], reducers: { itemAdded(state, action: PayloadAction<CartItem>) { const existing = state.find(i => i.id === action.payload.id); if (existing) existing.qty += action.payload.qty; // Immer allows "mutating" syntax safely else state.push(action.payload); }, itemRemoved(state, action: PayloadAction<number>) { return state.filter(i => i.id !== action.payload); }, }, }); export const { itemAdded, itemRemoved } = cartSlice.actions; export const store = configureStore({ reducer: { cart: cartSlice.reducer } }); // In components (react-redux hooks) const items = useSelector((s: RootState) => s.cart); const dispatch = useDispatch(); dispatch(itemAdded({ id: 1, name: 'Mouse', qty: 1 })); Wrap the app with <Provider store={store}>. RTK also includes createAsyncThunk for async logic and RTK Query for data fetching and caching. Benefits - Predictability: state changes only through actions and pure reducers. - Debugging: Redux DevTools show every action and allow time-travel debugging. - Centralised state shared across distant components without prop drilling. - Scales well for large teams with consistent patterns. - Middleware ecosystem and good testability (reducers are pure functions). When to use Redux - Large applications where many components share and update complex state (shopping carts, multi-step workflows, collaborative editing, complex dashboards). - When you need strong debugging, auditing of state changes or a strict architecture for a big team. - When the app already uses Redux and RTK Query for server data. When you probably do not need it - Small and medium apps where local state (useState, useReducer) and Context are enough. - Server data: libraries like TanStack Query or RTK Query handle caching and fetching better than hand-written Redux slices. - Lighter alternatives for client state: Zustand, Jotai, or React's built-in tools. Common interview follow-ups - Context vs Redux: Context passes values down the tree but has no built-in update patterns, devtools or selector-based subscriptions; every consumer re-renders when the value changes. - Why reducers must be pure: predictable results, easy testing and time-travel debugging.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics