After clicking on "Click me" button my count value is updating. Now I have to Do Something if count is grater then 0 before my component unmount.
But I have noticed during debugger count value is always 0. While I was expecting count should be grater then 0 if I clicked multiple times.
Please help me, how to get updated value during component unmount. Thanks
import React, { useState, useEffect } from 'react';
function Example() {
const [count, setCount] = useState(0);
useEffect(() => {
//ComponentDidMount
return(()=>{
//componentWillUnmount
alert(count); //count 0
if(count){
//Do Something
}
})
},[]);
useEffect(() => {
document.title = `You clicked ${count} times`;
});
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}