Member-only story
You Probably Don’t Need useEffect for These 5 React Problems
Shayan Ali
8 min read12 hours ago
Press enter or click to view image in full size
Many React developers use useEffect to fix problems that React never had. Here are five common patterns you can simplify with better state management.
If you have worked with React for a while, you have probably written something like this:
const [fullName, setFullName] = useState('');
useEffect(() => {
setFullName(firstName + ' ' + lastName);
}, [firstName, lastName]);
It looks reasonable.
Whenever the first or last name changes, the Effect updates the full name. The code works, and the UI displays the expected result.
But there is a problem.
You are storing a value that React can already calculate for you.
This is one of the most common mistakes I see in React code: using useEffect as a general-purpose tool for keeping state synchronized.
The problem is not that useEffect is bad.…