Reilly O'Donnell
Reilly's Blog

Follow

Reilly's Blog

Follow
⚛ React Tips - You're misusing the React useEffect hook (probably)

Photo by Jeremy Bishop on Unsplash

⚛ React Tips - You're misusing the React useEffect hook (probably)

Reilly O'Donnell's photo
Reilly O'Donnell
·Jan 6, 2023·

1 min read

This is def one of the most common mistakes I've seen in both personal and enterprise applications.

The useEffect hook exists to sync an external system (i.e. network, DOM) to your React application. That means when you're useEffect'ing to:
1. Adjust state or props when a certain value changes (in state or props)
2. Replicate legacy class based lifecycle methods

you're just doing it wrong.

With awesome packages like TanStack Query (React Query) & SWR you don't even need a useEffect to fetch data.

If you want to learn more about useEffect check out the new beta documentation on it: https://beta.reactjs.org/learn/you-might-not-need-an-effect

 
Share this