You’re building a dashboard app with a UserList component that renders a list of 1,000 users. The parent component, Dashboard, has a state filter that changes when the user types in a search bar. Here’s the code:
UserList
Dashboard
filter
import React from 'react'; const UserList = ({ users }) => { console.log('UserList rendered'); return ( <ul> {users.map(user => ( <li key={user.id}>{user.name}</li> ))} </ul> ); }; const Dashboard = () => { const [filter, setFilter] = React.useState(''); const users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]; // Imagine 1,000 users return ( <div> <input value={filter} onChange={e => setFilter(e.target.value)} /> <UserList users={users} /> </div> ); }; export default Dashboard;
Every time the user types in the input field, UserList re-renders, even though the users prop doesn’t change. Which of the following could help optimize this?
users
Move the users array outside the Dashboard component to make it a constant.
Add a useEffect hook in UserList to log renders only when users changes.
useEffect
Rewrite UserList as a class component instead of a functional component.
Wrap UserList with React.memo to prevent re-renders when users is unchanged.
React.memo
Replace the input with a button to reduce state updates.
input
Consider this code where a Timer component displays a static message, but it re-renders every time the parent’s count changes:
Timer
count
import React from 'react'; const Timer = ({ message }) => { console.log('Timer rendered'); return <div>{message}</div>; }; const App = () => { const [count, setCount] = React.useState(0); return ( <div> <button onClick={() => setCount(count + 1)}>Count: {count}</button> <Timer message="Time is ticking!" /> </div> ); };
You wrap Timer with React.memo. What happens when you click the button?
Timer re-renders because the parent (App) re-renders.
App
Timer doesn’t re-render because its message prop hasn’t changed.
message
Timer re-renders only if the count value is passed as a prop to it.
The console log stops working because React.memo disables side effects.
Imagine you’re optimizing a large e-commerce app. The ProductCard component displays product details and is used in a ProductList. Here’s a simplified version:
ProductCard
ProductList
import React from 'react'; const ProductCard = ({ product }) => { console.log(`ProductCard ${product.id} rendered`); return <div>{product.name} - ${product.price}</div>; }; const ProductList = () => { const [cart, setCart] = React.useState([]); const products = [ { id: 1, name: 'Laptop', price: 999 }, { id: 2, name: 'Phone', price: 599 }, ]; return ( <div> <button onClick={() => setCart([...cart, products[0]])}> Add Laptop to Cart </button> {products.map(product => ( <ProductCard key={product.id} product={product} /> ))} </div> ); };
When you click "Add Laptop to Cart," all ProductCard components re-render. How can you optimize this?
Store products in a useState hook instead of a plain array.
products
useState
Use useCallback to memoize the onClick handler.
useCallback
onClick
Wrap ProductCard with React.memo.
Add a shouldComponentUpdate method to ProductCard.
shouldComponentUpdate
You’re debugging a slow app and notice a component re-renders unnecessarily. Here’s the code:
import React from 'react'; const Display = React.memo(({ value }) => { console.log('Display rendered'); return <div>{value}</div>; }); const Counter = () => { const [count, setCount] = React.useState(0); const value = { number: 42 }; // Object created anew each render return ( <div> <button onClick={() => setCount(count + 1)}>Count: {count}</button> <Display value={value} /> </div> ); };
Even with React.memo, Display re-renders every time count changes. Why, and how can you fix it?
Display
The value object is recreated on every render, causing a prop change.
value
React.memo doesn’t work with object props.
Replace React.memo with React.PureComponent.
React.PureComponent
Move const value = { number: 42 } outside Counter or use useMemo.
const value = { number: 42 }
Counter
useMemo
In a real-world app, you’re rendering a list of comments. Each Comment component is wrapped with React.memo, but some still re-render unnecessarily. What might be causing this?
Comment
A prop like style={{ color: 'blue' }} is passed, creating a new object each render.
style={{ color: 'blue' }}
The comments data is fetched from an API and returned as a new array each time.
React.memo is broken and doesn’t work with lists.
The parent component passes a new function prop (e.g., onDelete) on every render.
onDelete
This content is only available to registered users.Please register at the the home page.