What causes unnecessary renders in React components?
Unnecessary renders happen when React re-renders a component even though its output would not change. This can be caused by changing state or props that don't affect the UI, or by passing new object or function references every time.
Click to reveal answer
beginner
How does React.memo help avoid unnecessary renders?
React.memo is a higher-order component that wraps a functional component. It tells React to skip re-rendering the component if its props have not changed, improving performance by avoiding work when not needed.
Click to reveal answer
intermediate
What is the purpose of useCallback in React?
useCallback returns a memoized version of a callback function that only changes if its dependencies change. This helps prevent passing new function references on every render, which can cause child components to re-render unnecessarily.
Click to reveal answer
intermediate
Why should you avoid creating new objects or arrays inside component bodies without memoization?
Creating new objects or arrays inside components causes their references to change every render. If these are passed as props, React thinks the props changed and re-renders child components. Memoizing them with useMemo or useCallback avoids this.
Click to reveal answer
intermediate
What role does useMemo play in avoiding unnecessary renders?
useMemo memoizes the result of a calculation or object creation so React reuses the same value between renders unless dependencies change. This prevents passing new references that cause child components to re-render.
Click to reveal answer
Which React hook helps memoize a function to avoid unnecessary re-renders?
AuseCallback
BuseState
CuseEffect
DuseRef
✗ Incorrect
useCallback memoizes functions so their references stay the same unless dependencies change, helping avoid unnecessary renders.
What does React.memo do?
ARuns side effects after render
BPrevents a component from rendering if its props haven't changed
CAutomatically updates state
DCreates a new component
✗ Incorrect
React.memo skips rendering a component if its props are the same as before, improving performance.
Why can passing new object literals as props cause unnecessary renders?
ABecause React ignores objects
BBecause React caches all objects
CBecause objects are always equal
DBecause objects are compared by reference, new objects look different each time
✗ Incorrect
React compares props by reference, so new object literals create new references causing re-renders.
Which hook would you use to memoize a computed value to avoid recalculating it every render?
AuseState
BuseCallback
CuseMemo
DuseEffect
✗ Incorrect
useMemo memoizes computed values so they are recalculated only when dependencies change.
What is a common sign that a React component is rendering unnecessarily?
AThe UI looks the same but performance is slow
BThe component never updates
CThe component crashes
DThe component only renders once
✗ Incorrect
Unnecessary renders cause slow performance even though the UI output does not change.
Explain how React.memo, useCallback, and useMemo work together to avoid unnecessary renders.
Think about how React decides to re-render components based on props and function references.
You got /4 concepts.
Describe why creating new objects or functions inside a React component can cause performance issues.
Focus on how React compares props and why stable references matter.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using React.memo in a React component?
easy
A. To convert a class component into a functional component
B. To force the component to re-render every time the parent renders
C. To prevent re-rendering when the component's props have not changed
D. To automatically update the component's state
Solution
Step 1: Understand React.memo's role
React.memo is a higher-order component that wraps a functional component to optimize rendering.
Step 2: Identify its effect on rendering
It skips re-rendering the wrapped component if its props remain the same, improving performance.
Final Answer:
To prevent re-rendering when the component's props have not changed -> Option C
Quick Check:
React.memo skips renders if props unchanged = D [OK]
Hint: React.memo skips render if props stay the same [OK]
Common Mistakes:
Thinking React.memo forces re-render
Confusing React.memo with state updates
Believing it converts component types
2. Which of the following is the correct syntax to memoize a function using useCallback in React?
easy
A. const memoizedFn = useCallback(() => { doSomething(); }, []);
B. const memoizedFn = useCallback(doSomething(), []);
C. const memoizedFn = useCallback(() => doSomething);
D. const memoizedFn = useCallback(() => { doSomething(); });
Solution
Step 1: Recall useCallback syntax
useCallback takes a function and a dependency array, returning a memoized version of the function.
Step 2: Check each option's correctness
const memoizedFn = useCallback(() => { doSomething(); }, []); correctly passes a function and an empty dependency array, memoizing the function properly.
How can you fix the unnecessary re-renders of Child?
medium
A. Wrap increment in useCallback with count as dependency
B. Remove React.memo from Child
C. Move increment outside the Parent component
D. Use useMemo to memoize count
Solution
Step 1: Identify cause of re-renders
The increment function is recreated on every render, changing its reference and causing Child to re-render.
Step 2: Apply useCallback to memoize function
Wrapping increment in useCallback with count dependency keeps the same function reference unless count changes.
Final Answer:
Wrap increment in useCallback with count as dependency -> Option A
Quick Check:
Memoize functions with useCallback to avoid re-renders = A [OK]
Hint: Memoize event handlers with useCallback to prevent re-renders [OK]
Common Mistakes:
Removing React.memo which disables optimization
Moving function outside loses access to state
Using useMemo incorrectly for functions
5. You want to optimize a component that receives a large list and a filter function as props. The filter function is defined inline in the parent component. Which approach best avoids unnecessary renders of the child component?
hard
A. Use useMemo to memoize the filtered list inside the child component
B. Define the filter function inline without memoization and rely on React.memo alone
C. Avoid React.memo and always re-render the child component to ensure fresh data
D. Wrap the child component with React.memo and memoize the filter function using useCallback with proper dependencies
Solution
Step 1: Understand props causing re-renders
The inline filter function changes reference every render, causing child re-renders despite React.memo.
Step 2: Use React.memo and useCallback together
Wrapping child with React.memo and memoizing the filter function with useCallback keeps function reference stable, avoiding unnecessary renders.
Final Answer:
Wrap the child component with React.memo and memoize the filter function using useCallback with proper dependencies -> Option D
Quick Check:
Memoize functions and use React.memo to optimize renders = A [OK]
Hint: Memoize functions and wrap child with React.memo for best optimization [OK]
Common Mistakes:
Relying on React.memo without memoizing functions
Memoizing filtered list inside child instead of function
Avoiding React.memo and causing extra renders
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.