I'm currently looking at the react doc's example for useEffect
import React, { useState, useEffect } from 'react';
function Example() {
const [count, setCount] = useState(0);
// Similar to componentDidMount and componentDidUpdate:
useEffect(() => {
// Update the document title using the browser API
document.title = `You clicked ${count} times`;
});
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
My question is that we can easily make a handleClick function for the button. We don't have to use useEffect
const handleButtonClick = () =>{
setCount(count+1)
document.title = `You clicked ${count +1} times`
}
<button onClick={handleButtonClick}/>
So which way is considered good practice? Is it best to only use useEffect to trigger side effects that strictly couldn't be done along with the main effect(i.e when component receive new prop)