How to perform ComponentDidMount, ComponentDidUpadte and ComponentUnmount in functional component
Aug 1, 2023
In React functional components, you can achieve the equivalent behavior of componentDidMount
, componentDidUpdate
, and componentWillUnmount
lifecycle methods using the useEffect
hook.
1 componentDidMount
useEffect(() => {
console.log(“Mount”)},[])
2 componentDidUpdate
useEffect(() => {
console.log(“Mount”)},[setAnyVariableName])
3 componentWillUnmount
useEffect(() => { return()=>{ console.log(“Mount”)
}
},[])