Challenge - 5 Problems
Redux Slice Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output of this Redux slice state after dispatch?
Consider this Redux slice for a counter. What will be the value of
state.value after dispatching increment() twice?React Native
import { createSlice } from '@reduxjs/toolkit'; const counterSlice = createSlice({ name: 'counter', initialState: { value: 0 }, reducers: { increment: (state) => { state.value += 1 }, decrement: (state) => { state.value -= 1 } } }); const { increment } = counterSlice.actions; let state = { value: 0 }; state = counterSlice.reducer(state, increment()); state = counterSlice.reducer(state, increment()); console.log(state.value);
Attempts:
2 left
💡 Hint
Each increment adds 1 to the current value.
✗ Incorrect
The initial state value is 0. Each increment action adds 1, so after two increments, the value is 2.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a Redux slice with an action to reset state?
You want to create a Redux slice with an action
reset that sets state.value back to 0. Which code snippet is correct?Attempts:
2 left
💡 Hint
Reducers can mutate state directly when using createSlice.
✗ Incorrect
Option A correctly uses a function with a block that mutates state.value. Option A lacks braces, causing a syntax error. Option A returns a new object which is allowed but less common here. Option A has no state parameter, so state is undefined.
❓ lifecycle
advanced2:00remaining
What happens if you dispatch an unknown action to a Redux slice reducer?
Given a Redux slice reducer, what is the state output if an action with type
unknownAction is dispatched?React Native
const slice = createSlice({
name: 'test',
initialState: { count: 5 },
reducers: {
add: (state) => { state.count += 1 }
}
});
const unknownAction = { type: 'unknownAction' };
const newState = slice.reducer(slice.getInitialState(), unknownAction);
console.log(newState.count);Attempts:
2 left
💡 Hint
Reducers return current state if action type is not handled.
✗ Incorrect
Redux slice reducers return the current state unchanged if the action type is not recognized. So the count remains 5.
🔧 Debug
advanced2:00remaining
Why does this Redux slice reducer not update the state?
Look at this reducer code. Why does dispatching
increment() not change state.value?React Native
const slice = createSlice({
name: 'counter',
initialState: { value: 0 },
reducers: {
increment: (state) => { return { value: state.value + 1 } }
}
});
let state = slice.getInitialState();
state = slice.reducer(state, slice.actions.increment());
console.log(state.value);Attempts:
2 left
💡 Hint
Reducers can return new state objects instead of mutating.
✗ Incorrect
Returning a new object from a reducer replaces the state correctly. The output will be 1 as expected.
🧠 Conceptual
expert2:00remaining
Which statement about Redux Toolkit slices and actions is true?
Select the correct statement about Redux Toolkit slices and their generated actions.
Attempts:
2 left
💡 Hint
Immer allows safe state mutation in reducers.
✗ Incorrect
Redux Toolkit uses Immer, so reducers can safely mutate state directly. Actions do not always require payloads. Action types are auto-generated. Reducers can also return new state objects.