function Example() {
const [count, setCount] = React.useState(0);
React.useEffect(() => { // similar to componentDidMount() & componentDidUpdate()
document.title = `You clicked ${count} times`;
return ()=>{ // similar to componentWillUnmount() with []; optional
alert("Sorry to see you go!");
}
},[]); // prevents unintentional alert() call since no change in array;, optional
return (
You clicked {count} times
);}ReactDOM.render(,document.querySelector("div"));setTimeout(()=>{ // simulating unmounting after 5 seconds
ReactDOM.unmountComponentAtNode(document.querySelector('div'));},5000);