Challenge - 5 Problems
Redux Selector Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2: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);Attempts:
2 left
💡 Hint
Look for todos where completed is true.
✗ Incorrect
The selector filters todos to only those with completed set to true, so it returns the first and third todos.
🧠 Conceptual
intermediate1:30remaining
Why use selectors in Redux?
Which of the following is the main benefit of using selectors in a Redux app?
Attempts:
2 left
💡 Hint
Think about how selectors help with reading state.
✗ Incorrect
Selectors are functions that read and compute data from the Redux state, often improving performance by memoizing results.
📝 Syntax
advanced2: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;
Attempts:
2 left
💡 Hint
createSelector takes input selectors and a result function.
✗ Incorrect
Option D correctly uses createSelector with an input selector and a function that returns the count of completed todos.
❓ lifecycle
advanced1:40remaining
When are selectors recomputed?
Given a memoized selector created with Reselect, when will it recompute its output?
Attempts:
2 left
💡 Hint
Memoization avoids recomputing if inputs are unchanged.
✗ Incorrect
Memoized selectors recompute only when their input selectors return new values, improving performance.
🔧 Debug
expert2: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);Attempts:
2 left
💡 Hint
Check what happens if state.todos is missing.
✗ Incorrect
If state.todos is undefined, calling filter on it throws a TypeError since undefined has no filter method.