useeffect hooks react unmount code example
Example 1: useeffect componentdidmount
import React, { useState, useEffect } from 'react';
function Example() {
const [count, setCount] = useState(0);
// Similar to componentDidMount and componentDidUpdate:
useEffect(() => {
// Update the document title using the browser API
document.title = `You clicked ${count} times`;
});
);
}
Example 2: useeffect cleanup function
function App() {
const [shouldRender, setShouldRender] = useState(true);
useEffect(() => {
setTimeout(() => {
setShouldRender(false);
}, 5000);
}, []);
// don't render
if( !shouldRender ) return null;
// JSX, if the shouldRender is true
return <ForExample />;
}