In the reactjs UseEffect documentation, we find below code where the document.title is updated using the useEffect hook
import React, { useState, useEffect } from 'react';
function Example() {
const [count, setCount] = useState(0);
useEffect(() => {
document.title = `You clicked ${count} times`;
});
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
export default App;
I am trying to understand the difference between using the useEffect hook without passing the second argument ( like [] for mount only) and just using the code to update the title (or any other code) outside without using the hook.
import { useState, useEffect } from 'react';
function App() {
const [count, setCount] = useState(0);
document.title = `You clicked ${count} times`;
// useEffect(() => {
// document.title = `You clicked ${count} times`;
// });
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
export default App;
What is the difference?