0
0
React Nativemobile~20 mins

Redux selectors in React Native - Practice Problems & Coding Challenges

Choose your learning style9 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.