0
0
Reactframework~5 mins

What is useEffect in React - Quick Revision & Key Takeaways

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the useEffect hook in React?

useEffect lets you run code after your component renders. It helps you handle side effects like fetching data, updating the DOM, or setting timers.

Click to reveal answer
beginner
How do you control when useEffect runs?

You pass a dependency array as the second argument to useEffect. It runs the effect only when values in this array change.

Click to reveal answer
beginner
What happens if you pass an empty array [] to useEffect?
<p>The effect runs only once after the first render, similar to <code>componentDidMount</code> in class components.</p>
Click to reveal answer
intermediate
Why might you return a function from inside useEffect?

Returning a function lets you clean up side effects, like removing event listeners or clearing timers, before the component unmounts or before the effect runs again.

Click to reveal answer
beginner
Give an example of a side effect you might handle with useEffect.

Fetching data from an API when the component loads, updating the document title, or subscribing to a WebSocket are common side effects handled with useEffect.

Click to reveal answer
What does useEffect do in a React component?
AStyles the component
BCreates a new component
CManages component state
DRuns code after rendering to handle side effects
When does useEffect run if you provide an empty dependency array []?
AOnly once after the first render
BAfter every render
CNever
DOnly when the component unmounts
What is the purpose of returning a function inside useEffect?
ATo update the component state
BTo fetch data
CTo clean up side effects before next run or unmount
DTo render JSX
Which hook is used to handle side effects in React functional components?
AuseState
BuseEffect
CuseContext
DuseRef
If you want useEffect to run only when a specific variable changes, what do you do?
APass that variable in the dependency array
BCall <code>useEffect</code> multiple times
CUse <code>useState</code> instead
DPut the variable inside the effect function only
Explain in your own words what useEffect does and why it is useful in React.
Think about things like fetching data or timers in a component.
You got /4 concepts.
    Describe how you would use useEffect to fetch data only once when a component loads.
    Remember the empty array means run once.
    You got /4 concepts.