Redux Example — React Technical Interview Questions & Answers
Redux Example
Redux Example
- Tiny example: cart reducer. state is a string[]. action { type: 'cart/add', payload: 'Pen' }. return [...state, action.payload]. Never state.push. Dispatch from a button. The screen lists the array. Same pattern as a counter: inc action, n+1.
- Walk dispatch → reducer → new store → React-Redux useSelector re-render. That four-step dry run is the ‘Redux example’ viva.
- Redux Example — output: Pen. payload is the item. Spread a new array — don’t push on state.
view click
│ dispatch
▼
reducer
│ new state
▼
store → UITechnical Interview Tip: One add-to-cart reducer on the board.
Code Examples for Redux Example
Example
// Redux cart
const add = { type: "cart/add", payload: "Pen" };
function cart(state = [], action) {
if (action.type === "cart/add") return [...state, action.payload];
return state;
}
console.log(cart([], add).join(","));
Redux Example — output: Pen. payload is the item. Spread a new array — don’t push on state.
How to Explain Redux Example to an Interviewer
Explain Redux Example as if you are teaching a junior — definition, then one tiny UI.
Action : type + payload. Reducer : copy, don’t mutate.
What does the example show on screen (or print), and what does that prove?
Redux Example — output: Pen
What mistake do freshers make with Redux Example?
Reducer : copy, don’t mutate.