0
0
React Nativemobile~20 mins

Redux slices and actions in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Redux Slice Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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);
A2
B1
C0
D-2
Attempts:
2 left
💡 Hint
Each increment adds 1 to the current value.
📝 Syntax
intermediate
2: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?
Aconst slice = createSlice({ name: 'counter', initialState: { value: 0 }, reducers: { reset: (state) => { state.value = 0 } } });
Bconst slice = createSlice({ name: 'counter', initialState: { value: 0 }, reducers: { reset: () => { state.value = 0 } } });
Cconst slice = createSlice({ name: 'counter', initialState: { value: 0 }, reducers: { reset: (state) => { return { value: 0 } } } });
Dconst slice = createSlice({ name: 'counter', initialState: { value: 0 }, reducers: { reset: (state) => state.value = 0 } });
Attempts:
2 left
💡 Hint
Reducers can mutate state directly when using createSlice.
lifecycle
advanced
2: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);
Aundefined
B6
C5
DThrows an error
Attempts:
2 left
💡 Hint
Reducers return current state if action type is not handled.
🔧 Debug
advanced
2: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);
AThe reducer should mutate state directly; returning a new object causes state to reset to initial.
BThe reducer returns a new object, but Immer ignores it, so state does not update.
CThe reducer returns a new object but Immer expects mutation or full replacement; this works correctly.
DThe reducer returns a new object which replaces state correctly; output will be 1.
Attempts:
2 left
💡 Hint
Reducers can return new state objects instead of mutating.
🧠 Conceptual
expert
2:00remaining
Which statement about Redux Toolkit slices and actions is true?
Select the correct statement about Redux Toolkit slices and their generated actions.
AActions generated by createSlice always require a payload argument.
BReducers in createSlice can mutate state directly because of Immer integration.
CYou must manually define action types when using createSlice.
DcreateSlice reducers cannot return new state objects; they must mutate state.
Attempts:
2 left
💡 Hint
Immer allows safe state mutation in reducers.