0
0
Reactframework~5 mins

Lifecycle mapping with hooks in React - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What React hook is used to run code after the component first renders?
The <code>useEffect</code> hook with an empty dependency array (<code>[]</code>) runs code after the first render, similar to <code>componentDidMount</code> in class components.
Click to reveal answer
beginner
How do you run cleanup code when a React component unmounts using hooks?
Inside useEffect, return a function. This returned function runs when the component unmounts, similar to componentWillUnmount.
Click to reveal answer
intermediate
Which hook replaces componentDidUpdate behavior?
The useEffect hook runs after every render by default. To mimic componentDidUpdate, use useEffect with specific dependencies to run code when those values change.
Click to reveal answer
beginner
What does the dependency array in useEffect control?
The dependency array tells React when to re-run the effect. If empty ([]), it runs once after mount. If it has variables, it runs when those variables change.
Click to reveal answer
intermediate
How can you run code only when a specific prop or state changes in a functional component?
Use useEffect with that prop or state in the dependency array. React runs the effect only when those values change.
Click to reveal answer
Which hook is primarily used to handle lifecycle events in React functional components?
AuseState
BuseRef
CuseContext
DuseEffect
What happens if you omit the dependency array in useEffect?
AEffect runs only once after mount
BEffect runs after every render
CEffect never runs
DEffect runs only on unmount
How do you run cleanup code when a component unmounts?
AReturn a function inside <code>useEffect</code>
BUse <code>useState</code> to reset values
CCall cleanup in the component body
DUse <code>useRef</code> to track unmount
To run an effect only when a specific state changes, what should you do?
ADo not use <code>useEffect</code>
BLeave the dependency array empty
CAdd that state to the dependency array
DUse <code>useState</code> instead
Which lifecycle method does useEffect(() => {}, []) mimic?
AcomponentDidMount
BcomponentWillUnmount
CcomponentDidUpdate
Drender
Explain how you can use React hooks to mimic the lifecycle methods: componentDidMount, componentDidUpdate, and componentWillUnmount.
Think about how useEffect runs after render and how dependencies control when it runs.
You got /3 concepts.
    Describe the role of the dependency array in the useEffect hook and how it affects when the effect runs.
    Consider how React decides when to re-run the effect.
    You got /3 concepts.