In the React docs I see this piece of code:
function Example() {
const [count, setCount] = useState(0);
//THE SUBJECT OF MY QUESTION:
useEffect(() => {
document.title = `You clicked ${count} times`;
});
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
So, my question is why not change the document.title or any other DOM without using useEffect, like this:
function Example() {
const [count, setCount] = useState(0);
//THE SUBJECT OF MY QUESTION:
document.title = `You clicked ${count} times`;
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
I know that when sending requests for example, it is asynchronous and we need to do it in useEffect. But DOM manipulation isn't asynchronous and it takes relatively 0 time, why do we then still have to use useEffect hook?