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
javascriptDefine a counter slice with Redux Toolkit.
const counterSlice = createSlice({
name: "counter",
initialState: { value: 0 },
reducers: {
increment(state) {
state.value += 1;
},
},
}); 2. Configure Store
javascriptConfigure a store with the slice reducer.
export const store = configureStore({
reducer: {
counter: counterSlice.reducer,
},
}); 3. Typed Hooks
typescriptCreate typed useAppDispatch/useAppSelector.
export const useAppDispatch = useDispatch.withTypes<AppDispatch>();
export const useAppSelector = useSelector.withTypes<RootState>(); 4. Selector
javascriptWrite a memoized selector.
export const selectCount = (state) => state.counter.value; 5. Async Thunk
javascriptCreate 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
javascriptHandle pending/fulfilled in extraReducers.
extraReducers: (builder) => {
builder.addCase(fetchUsers.fulfilled, (state, action) => {
state.items = action.payload;
});
} 7. Provider Wrap
jsxWrap the app with Provider.
<Provider store={store}>
<App />
</Provider> 8. Dispatch Action
jsxDispatch an increment action from a component.
const dispatch = useAppDispatch();
<button onClick={() => dispatch(increment())}>+</button> 9. Normalized State
javascriptStore entities by id.
const initialState = {
ids: [],
entities: {},
}; 10. Immer Update
javascriptUpdate nested state safely in a slice.
reducers: {
rename(state, action) {
state.entities[action.payload.id].name = action.payload.name;
},
} 11. Listener Middleware
javascriptReact to an action with listenerMiddleware.
listenerMiddleware.startListening({
actionCreator: login.fulfilled,
effect: async (action, api) => {
localStorage.setItem("token", action.payload.token);
},
}); 12. Reset State
javascriptReset slice state on logout.
extraReducers: (builder) => {
builder.addCase(logout, () => initialState);
}