Skip to content

Coding practice

Redux Coding Questions

12 hands-on challenges with prompts and solution sketches for Redux interview rounds.

Challenge set

12 coding questions

1. Create Slice

javascript

Define a counter slice with Redux Toolkit.

const counterSlice = createSlice({
  name: "counter",
  initialState: { value: 0 },
  reducers: {
    increment(state) {
      state.value += 1;
    },
  },
});

2. Configure Store

javascript

Configure a store with the slice reducer.

export const store = configureStore({
  reducer: {
    counter: counterSlice.reducer,
  },
});

3. Typed Hooks

typescript

Create typed useAppDispatch/useAppSelector.

export const useAppDispatch = useDispatch.withTypes<AppDispatch>();
export const useAppSelector = useSelector.withTypes<RootState>();

4. Selector

javascript

Write a memoized selector.

export const selectCount = (state) => state.counter.value;

5. Async Thunk

javascript

Create an async thunk for fetching users.

export const fetchUsers = createAsyncThunk("users/fetch", async () => {
  const res = await fetch("/api/users");
  return res.json();
});

6. Extra Reducers

javascript

Handle pending/fulfilled in extraReducers.

extraReducers: (builder) => {
  builder.addCase(fetchUsers.fulfilled, (state, action) => {
    state.items = action.payload;
  });
}

7. Provider Wrap

jsx

Wrap the app with Provider.

<Provider store={store}>
  <App />
</Provider>

8. Dispatch Action

jsx

Dispatch an increment action from a component.

const dispatch = useAppDispatch();
<button onClick={() => dispatch(increment())}>+</button>

9. Normalized State

javascript

Store entities by id.

const initialState = {
  ids: [],
  entities: {},
};

10. Immer Update

javascript

Update nested state safely in a slice.

reducers: {
  rename(state, action) {
    state.entities[action.payload.id].name = action.payload.name;
  },
}

11. Listener Middleware

javascript

React to an action with listenerMiddleware.

listenerMiddleware.startListening({
  actionCreator: login.fulfilled,
  effect: async (action, api) => {
    localStorage.setItem("token", action.payload.token);
  },
});

12. Reset State

javascript

Reset slice state on logout.

extraReducers: (builder) => {
  builder.addCase(logout, () => initialState);
}