Discover how a simple helper can save you hours of debugging and speed up your app!
Why Redux selectors in React Native? - Purpose & Use Cases
Imagine you have a big app with lots of data stored in one place. To get just the piece you want, you dig through all the data every time you need it.
For example, you want to show a user's name on the screen, so you search through the whole data every time the screen updates.
This way is slow and messy. You repeat the same searching code in many places. If the data changes shape, you must fix all those places. It's easy to make mistakes and hard to keep track.
Redux selectors are like helpers that know exactly where to find the data you want. You write the search once, and then just call the helper everywhere.
This keeps your code clean, fast, and easy to update.
const userName = state.users.find(u => u.id === userId).name;
const selectUserName = (state, userId) => state.users.find(u => u.id === userId)?.name; const userName = selectUserName(state, userId);
You can build apps that stay fast and easy to maintain, even as they grow bigger and more complex.
Think of a shopping app where you show product details in many places. Using selectors, you write the logic to get product info once, then reuse it everywhere without repeating yourself.
Manual data searching is slow and error-prone.
Selectors let you write data access code once and reuse it.
This makes your app faster, cleaner, and easier to maintain.
Practice
Solution
Step 1: Understand Redux store role
The Redux store holds the entire app state in one place.Step 2: Role of selectors
Selectors are functions that pick specific parts of that state for use in components.Final Answer:
To extract specific data from the Redux store state -> Option BQuick Check:
Selector = extract data [OK]
- Confusing selectors with action creators
- Thinking selectors update state
- Using selectors to create UI components
selectUser that returns state.user?Solution
Step 1: Check function parameter
A selector receives the whole state as an argument, so it must havestateparameter.Step 2: Check return statement
The selector returnsstate.userdirectly. Arrow function with implicit return is correct.Final Answer:
const selectUser = state => state.user; -> Option DQuick Check:
Arrow function with state param and return [OK]
- Omitting the state parameter
- Not returning the value properly
- Using braces without return statement
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?Solution
Step 1: Understand selector logic
The selector filters todos wherecompletedis true.Step 2: Apply filter to given state
Todos with id 1 and 3 havecompleted:true, so they are included.Final Answer:
[{id:1, completed:true}, {id:3, completed:true}] -> Option AQuick Check:
Filter completed todos = correct list [OK]
- Selecting incomplete todos instead
- Returning empty array by mistake
- Confusing filter with map
const selectUserName = state => { state.user.name }Solution
Step 1: Check function body
The function uses braces but no return statement, so it returns undefined.Step 2: Fix by adding return
Either remove braces or addreturnto returnstate.user.name.Final Answer:
It does not return any value explicitly -> Option AQuick Check:
Missing return in arrow function with braces [OK]
- Forgetting return inside braces
- Confusing selectors with actions
- Using wrong parameter names
reselect to get the count of completed todos from state. Which code correctly implements this?Solution
Step 1: Use correct import and createSelector
ThecreateSelectorfunction is imported from 'reselect' and used properly.Step 2: Define input selector and output function
selectTodosgets todos array; output function filters completed and counts them.Step 3: Check other options for errors
The plain selectorconst selectCompletedCount = state => state.todos.filter(todo => todo.completed).lengthis not memoized; importingcreateSelectorfrom'redux'is wrong;todos => todos.completed.lengthaccesses property incorrectly.Final Answer:
import { createSelector } from 'reselect'; const selectTodos = state => state.todos; const selectCompletedCount = createSelector(selectTodos, todos => todos.filter(todo => todo.completed).length); -> Option CQuick Check:
Use reselect createSelector with input and output functions [OK]
- Importing createSelector from wrong package
- Not memoizing selector
- Incorrectly accessing properties in output function
Ready to prove it?
Pass the test and this pattern moves unknown
