Articles → REACT.JS → Clean Up Function Of The Useeffect Hooks In React.Js
Clean Up Function Of The Useeffect Hooks In React.Js
Purpose
Syntax
useEffect(() => {
// This is the effect itself.
return () => {
// This is its cleanup.
};
});
Example
import React, { useState, useEffect } from 'react';
import ReactDOM from 'react-dom';
const Home = () => {
const [name, setName] = useState('Gyan');
useEffect(() => {
console.log("this is invoked when the content is rendered");
return () => {
console.log("useeffect return");
};
});
const handleNameClick = () => {
setName('Gyansangrah');
}
return (
<div>
<p>{name}</p>
<button onClick={handleNameClick}>Set Name</button>
</div>
);
}
ReactDOM.render(<Home />, document.getElementById('root'));
Output
Click to Enlarge