When NOT to Use useEffect
RE · useEffectsyntax
// Derived state: compute during render
const fullName = firstName + ' ' + lastName;
// Event response: handle in the event handler
function handleClick() { doSomething(); }example
// BAD: useEffect to sync derived data
const [firstName, setFirstName] = useState('Kai');
const [lastName, setLastName] = useState('Chen');
const [fullName, setFullName] = useState('');
useEffect(() => {
setFullName(firstName + ' ' + lastName);
}, [firstName, lastName]);
// GOOD: compute directly during render
const fullName = firstName + ' ' + lastName;
// BAD: useEffect for event-driven logic
useEffect(() => {
if (submitted) sendAnalytics();
}, [submitted]);
// GOOD: call directly in the event handler
function handleSubmit() {
submitForm();
sendAnalytics();
}Note Overusing useEffect is one of the most common React mistakes. If you can calculate something from existing props or state, do it during render. If something should happen in response to a user action, put it in the event handler.