0
0
Reactframework~5 mins

Dependency array usage in React - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the dependency array in React's useEffect hook?
The dependency array tells React when to run the effect. React runs the effect only if one of the dependencies has changed since the last render.
Click to reveal answer
beginner
What happens if you pass an empty dependency array ([]) to useEffect?
The effect runs only once after the first render, similar to componentDidMount in class components.
Click to reveal answer
beginner
What is the effect of omitting the dependency array in useEffect?
The effect runs after every render, which can cause performance issues or unwanted repeated actions.
Click to reveal answer
intermediate
Why should you include all variables used inside useEffect in its dependency array?
Including all variables ensures the effect updates correctly when those variables change, preventing bugs and stale data.
Click to reveal answer
intermediate
What can happen if you incorrectly specify dependencies in the dependency array?
The effect might not run when it should, or it might run too often, causing bugs or performance problems.
Click to reveal answer
What does an empty dependency array in useEffect mean?
ARun effect after every render
BRun effect only once after first render
CRun effect only when dependencies change
DNever run the effect
If you omit the dependency array in useEffect, when does the effect run?
AAfter every render
BOnly when dependencies change
COnly once after first render
DNever
Which of these should be included in the dependency array?
AOnly state variables
BOnly props
CAll variables used inside the effect
DNo variables
What problem can occur if dependencies are missing in the array?
ANo problem
BEffect never runs
CEffect runs too often
DEffect uses stale data
How can you avoid infinite loops with useEffect?
AInclude all dependencies correctly
BNever use useEffect
CUse empty dependency array always
DCall setState inside useEffect without dependencies
Explain how the dependency array controls when useEffect runs in React.
Think about when React decides to run your effect based on the array.
You got /3 concepts.
    Describe common mistakes with dependency arrays and how to avoid them.
    Consider what happens if you forget to list variables or list wrong ones.
    You got /3 concepts.