Suppose I have the following simple app:
const App = (props) => {
const [count, setCount] = useState(0);
useEffect(() => {
console.log("before setting ", count);
setCount(count + 1);
console.log("set to ", count);
}, []);
return <div>hello</div>;
}
The expected result is:
before setting 0
set to 0
but the actual result is:
before setting 0
set to 0
before setting 0
set to 0
I do not understand two things:
- why does the
useEffect
function run twice? - why does not setting the count value actually set it?
Thank you in advance!