A Redux slice is a part of the Redux store that manages a specific piece of state. It includes the state, reducers (functions to change state), and actions all bundled together.
Click to reveal answer
beginner
What is an action in Redux?
An action is a plain JavaScript object that describes a change or event in the app. It usually has a type property and sometimes a payload with extra data.
Click to reveal answer
intermediate
How do you create a slice using Redux Toolkit?
You use the createSlice function, which takes an object with name, initialState, and reducers. It automatically generates action creators and action types for you.
Click to reveal answer
intermediate
Why are actions important in Redux slices?
Actions tell the reducers what happened so they can update the state accordingly. In slices, actions are generated automatically to keep code simple and consistent.
Click to reveal answer
beginner
What does a reducer function do inside a Redux slice?
A reducer function takes the current state and an action, then returns a new state based on the action. It must not change the state directly but return a new copy.
Click to reveal answer
What does createSlice automatically generate for you?
AAction creators and action types
BOnly the initial state
CUI components
DMiddleware functions
✗ Incorrect
createSlice generates action creators and action types based on the reducers you define.
Which property is required in every Redux action?
Apayload
Breducer
Cstate
Dtype
✗ Incorrect
Every Redux action must have a 'type' property to describe the action.
Where do you define how state changes in a Redux slice?
AIn the store configuration
BIn the UI components
CIn the reducers inside the slice
DIn the middleware
✗ Incorrect
Reducers inside the slice define how the state changes in response to actions.
What is the main benefit of using Redux Toolkit's createSlice?
AIt simplifies writing Redux logic by bundling state, reducers, and actions
BIt replaces React hooks
CIt automatically creates UI components
DIt removes the need for reducers
✗ Incorrect
createSlice bundles state, reducers, and actions to simplify Redux logic.
In Redux, what must a reducer never do?
AReturn a new state
BModify the state directly
CHandle actions
DBe a pure function
✗ Incorrect
Reducers must never modify the state directly; they should return a new state object.
Explain what a Redux slice is and how it helps organize state management.
Think about how a slice groups related logic together.
You got /5 concepts.
Describe the role of actions and reducers in updating the Redux store.
Consider how actions and reducers work together to change state.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a Redux slice in a React Native app?
easy
A. To create UI components automatically
B. To style the app with CSS
C. To handle navigation between screens
D. To group related state and actions together for easier management
Solution
Step 1: Understand Redux slice role
A slice groups state and actions related to a feature in one place.
Step 2: Compare with other options
Options A, C, and D are unrelated to state management.
Final Answer:
To group related state and actions together for easier management -> Option D
Quick Check:
Redux slice = group state and actions [OK]
Hint: Slices bundle state and actions for one feature [OK]
Common Mistakes:
Confusing slices with UI components
Thinking slices handle navigation
Assuming slices style the app
2. Which of the following is the correct way to define an action inside a Redux slice using createSlice?
Reducers are defined as functions with (state) parameter that mutate state directly.
Step 2: Check each option
reducers: { increment: (state) => { state.value += 1 } } correctly uses reducers with state mutation. Options B and D wrongly use 'actions' key. reducers: { increment: () => state.value += 1 } uses arrow function without braces, which won't mutate state properly.
What is the most likely reason the UI does not update?
medium
A. The reducer mutates state directly which is not allowed
B. The initialState should be an object, not an array
C. The slice reducer is not added to the store
D. The action payload is missing
Solution
Step 1: Check reducer code
The reducer correctly mutates state array by pushing new todo, which is allowed with Immer in createSlice.
Step 2: Identify missing integration
If UI does not update, likely the slice reducer is not connected to the Redux store, so dispatch has no effect.
Final Answer:
The slice reducer is not added to the store -> Option C
Quick Check:
Reducer must be in store for dispatch to update state [OK]
Hint: Always add slice reducer to store for updates [OK]
Common Mistakes:
Thinking direct mutation is forbidden in createSlice
Assuming initialState must be object
Forgetting to add reducer to store
5. You want to create a Redux slice that manages a list of users and includes an action to update a user's name by id. Which code snippet correctly implements this?
Reducers can mutate state directly because Immer handles immutability internally.
Step 2: Analyze each option
reducers: { updateName: (state, action) => { const user = state.find(u => u.id === action.payload.id); if(user) { user.name = action.payload.name; } } } finds user by id and mutates name directly, which is correct. reducers: { updateName: (state, action) => { state.map(u => u.id === action.payload.id ? { ...u, name: action.payload.name } : u); } } executes map but discards the new array without returning it or mutating state, incorrect. reducers: { updateName: (state, action) => { state = state.map(u => u.id === action.payload.id ? { ...u, name: action.payload.name } : u); } } tries to assign new array to state variable, which does not update state correctly. reducers: { updateName: (state, action) => { state.filter(u => u.id !== action.payload.id).push(action.payload); } } filters out user and pushes new one, which mutates array incorrectly and may cause bugs.