Bird
Raised Fist0
React Nativemobile~20 mins

Redux selectors in React Native - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redux Selector Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2:00remaining
What does this selector return?
Given the Redux state below, what will the selector selectCompletedTodos return?
React Native
const state = {
  todos: [
    { id: 1, text: 'Buy milk', completed: true },
    { id: 2, text: 'Walk dog', completed: false },
    { id: 3, text: 'Read book', completed: true }
  ]
};

const selectCompletedTodos = state => state.todos.filter(todo => todo.completed);
A[{ id: 1, text: 'Buy milk', completed: true }, { id: 3, text: 'Read book', completed: true }]
B[{ id: 2, text: 'Walk dog', completed: false }]
C[{ id: 1, text: 'Buy milk', completed: false }, { id: 3, text: 'Read book', completed: false }]
D[]
Attempts:
2 left
💡 Hint
Look for todos where completed is true.
🧠 Conceptual
intermediate
1:30remaining
Why use selectors in Redux?
Which of the following is the main benefit of using selectors in a Redux app?
AThey help extract and compute derived data from the Redux state efficiently.
BThey replace reducers to update the state directly.
CThey store the entire app state in local storage.
DThey dispatch actions to modify the store.
Attempts:
2 left
💡 Hint
Think about how selectors help with reading state.
📝 Syntax
advanced
2:30remaining
Identify the correct selector syntax
Which option correctly defines a memoized selector using Reselect to get the count of completed todos?
React Native
import { createSelector } from 'reselect';

const selectTodos = state => state.todos;
A
const selectCompletedCount = createSelector(
  todos => todos.filter(todo => todo.completed).length
);
B
const selectCompletedCount = createSelector(
  state => state.todos,
  todos => todos.filter(todo => todo.completed)
);
C
const selectCompletedCount = createSelector(
  selectTodos,
  todos => todos.completed.length
);
D
const selectCompletedCount = createSelector(
  selectTodos,
  todos => todos.filter(todo => todo.completed).length
);
Attempts:
2 left
💡 Hint
createSelector takes input selectors and a result function.
❓ lifecycle
advanced
1:40remaining
When are selectors recomputed?
Given a memoized selector created with Reselect, when will it recompute its output?
AOnly when the component using it mounts.
BOnly when the input state slices it depends on change.
CEvery time any action is dispatched, regardless of state changes.
DWhen the Redux store is initialized.
Attempts:
2 left
💡 Hint
Memoization avoids recomputing if inputs are unchanged.
🔧 Debug
expert
2:00remaining
Why does this selector cause a runtime error?
Consider this selector code. Why does it cause a runtime error when the state.todos is undefined?
React Native
const selectCompletedTodos = state => state.todos.filter(todo => todo.completed);
ABecause todo.completed is not a valid property.
BBecause filter is not a function on arrays.
CBecause state.todos is undefined, calling filter on undefined causes a TypeError.
DBecause the selector must be asynchronous.
Attempts:
2 left
💡 Hint
Check what happens if state.todos is missing.

Practice

(1/5)
1. What is the main purpose of a Redux selector in a React Native app?
easy
A. To create UI components
B. To extract specific data from the Redux store state
C. To define the initial state of the store
D. To dispatch actions to update the store

Solution

  1. Step 1: Understand Redux store role

    The Redux store holds the entire app state in one place.
  2. Step 2: Role of selectors

    Selectors are functions that pick specific parts of that state for use in components.
  3. Final Answer:

    To extract specific data from the Redux store state -> Option B
  4. Quick Check:

    Selector = extract data [OK]
Hint: Selectors pick data from state, not change it [OK]
Common Mistakes:
  • Confusing selectors with action creators
  • Thinking selectors update state
  • Using selectors to create UI components
2. Which of the following is the correct syntax to define a simple Redux selector function named selectUser that returns state.user?
easy
A. const selectUser = () => state.user;
B. const selectUser = (state) => { state.user }
C. function selectUser() { return user; }
D. const selectUser = state => state.user;

Solution

  1. Step 1: Check function parameter

    A selector receives the whole state as an argument, so it must have state parameter.
  2. Step 2: Check return statement

    The selector returns state.user directly. Arrow function with implicit return is correct.
  3. Final Answer:

    const selectUser = state => state.user; -> Option D
  4. Quick Check:

    Arrow function with state param and return [OK]
Hint: Selector must take state and return part of it [OK]
Common Mistakes:
  • Omitting the state parameter
  • Not returning the value properly
  • Using braces without return statement
3. Given the selector const selectCompletedTodos = state => state.todos.filter(todo => todo.completed); and the state { todos: [{id:1, completed:true}, {id:2, completed:false}, {id:3, completed:true}] }, what will selectCompletedTodos(state) return?
medium
A. [{id:1, completed:true}, {id:3, completed:true}]
B. [{id:2, completed:false}]
C. []
D. undefined

Solution

  1. Step 1: Understand selector logic

    The selector filters todos where completed is true.
  2. Step 2: Apply filter to given state

    Todos with id 1 and 3 have completed:true, so they are included.
  3. Final Answer:

    [{id:1, completed:true}, {id:3, completed:true}] -> Option A
  4. Quick Check:

    Filter completed todos = correct list [OK]
Hint: Filter returns only items matching condition [OK]
Common Mistakes:
  • Selecting incomplete todos instead
  • Returning empty array by mistake
  • Confusing filter with map
4. What is wrong with this selector code?
const selectUserName = state => { state.user.name }
medium
A. It does not return any value explicitly
B. It uses wrong parameter name
C. It should dispatch an action instead
D. It should be a class method

Solution

  1. Step 1: Check function body

    The function uses braces but no return statement, so it returns undefined.
  2. Step 2: Fix by adding return

    Either remove braces or add return to return state.user.name.
  3. Final Answer:

    It does not return any value explicitly -> Option A
  4. Quick Check:

    Missing return in arrow function with braces [OK]
Hint: Arrow with braces needs explicit return [OK]
Common Mistakes:
  • Forgetting return inside braces
  • Confusing selectors with actions
  • Using wrong parameter names
5. You want to create a memoized selector using reselect to get the count of completed todos from state. Which code correctly implements this?
hard
A. import { createSelector } from 'redux'; const selectCompletedCount = createSelector(state => state.todos, todos => todos.length);
B. const selectCompletedCount = state => state.todos.filter(todo => todo.completed).length;
C. import { createSelector } from 'reselect'; const selectTodos = state => state.todos; const selectCompletedCount = createSelector( selectTodos, todos => todos.filter(todo => todo.completed).length );
D. const selectCompletedCount = createSelector(state => state.todos, todos => todos.completed.length);

Solution

  1. Step 1: Use correct import and createSelector

    The createSelector function is imported from 'reselect' and used properly.
  2. Step 2: Define input selector and output function

    selectTodos gets todos array; output function filters completed and counts them.
  3. Step 3: Check other options for errors

    The plain selector const selectCompletedCount = state => state.todos.filter(todo => todo.completed).length is not memoized; importing createSelector from 'redux' is wrong; todos => todos.completed.length accesses property incorrectly.
  4. Final Answer:

    import { createSelector } from 'reselect'; const selectTodos = state => state.todos; const selectCompletedCount = createSelector(selectTodos, todos => todos.filter(todo => todo.completed).length); -> Option C
  5. Quick Check:

    Use reselect createSelector with input and output functions [OK]
Hint: Use createSelector from 'reselect' with input and output functions [OK]
Common Mistakes:
  • Importing createSelector from wrong package
  • Not memoizing selector
  • Incorrectly accessing properties in output function
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard