r/reactjs • u/Meow_man1213 • Apr 30 '26
Discussion Finally realized how much i was abusing useEffect, and deleting them is the best feeling ever..
290
Upvotes
r/reactjs • u/Meow_man1213 • Apr 30 '26
137
u/michaelfrieze Apr 30 '26
LLM’s love to use useEffect for some reason, even when I tell it not to. It’s so annoying.
30
u/Suspicious-Name4273 Apr 30 '26
https://github.com/nickjvandyke/eslint-plugin-react-you-might-not-need-an-effect
25
u/azangru Apr 30 '26
I am having a hard time imagining how you would rewrite the example that you gave (which includes data fetching in response to props change) in such a way that it is simply a deriving of variables
57
u/AaronBonBarron Apr 30 '26
I swear to god React is just an entire library of footguns and overly complicated workarounds to things that shouldn't even be issues in the first place.
18
u/Cahnis Apr 30 '26
People won't RTFM even at gunpoint... have you read the rest of the docs after realizing how much you are f'ing yourself up?
2
u/Sad-Percentage5351 Apr 30 '26
Curious how other FE frameworks handle this like Vue, Svelte, etc. Not trying to start an argument, just curious
5
u/YanVe_ Apr 30 '26
Going through some of the now legacy code, that I wrote when I was learning functional React, I do occasionally find some really bad useEffects, ngl. But I don't understand how someone can misunderstand the system to such a degree, that they genuinely find that "You Might Not Need an Effect" section to be useful. The things there are pretty simple to figure out once you write a couple of more complicated hooks and components...
3
u/StickFigureFan Apr 30 '26
Unpopular opinion, but I liked the old lifecycle events.
Oh your components mounting: do some stuff
Oh your component updated? do some stuff
useEffect made it cleaner to have multiple effects/dependencies in a component, but it also made it more likely that you'd put too much stuff into a single component IMO.
2
u/[deleted] Apr 30 '26
So you just didn't learn react? useEffect calls are for side effects, hence the name. Use them for anything outside of JS like calling a browser API. Your code and every other function should otherwise have zero side effects not in a useEffect.
1
u/Turd_King Apr 30 '26
Thanks this is why models cannot write good react code - each use effect removed from the internet is a win for everyone
1
u/haywire Apr 30 '26
I remember a time when the shit show you are describing was what we should strive for because decoupling and reactivity or something.
Now we know the secret of react is to use it as little as possible.
1
u/LancelotLac Apr 30 '26
add in some hooks and you are a pro, TanstackQuery taught me how to use hooks
1
u/srg666 May 01 '26
I added a “do not use useeffect or you will be fired” skill and am in the process of making a lint rule banning it.
1
u/CatolicQuotes May 01 '26
Cool, here are pitfalls and recaps for react. Version 18, but still valid
https://github.com/tombohub/react-pitfalls
https://github.com/tombohub/react-recaps
1
u/louicoder May 01 '26
I resorted to using preact signals and mostly don't need useMemo, useCallback etc
1
u/kidshibuya May 01 '26
I recently converted an app that has useEffect everywhere (hundreds of components), changed them all to be derived. The result was amazing, went from .4s to render to .4s to render. Stunning.
1
u/germanicel May 02 '26
Maybe you should read the react docs before you write more code
1
u/DamienDoesItBetter May 02 '26
Good. Lets the LLMs spit out useEffects wherever possible so we can still be employable.
1
u/No_Section_5137 May 10 '26
Better to check react docs before writing code
1
u/tinkering-mind May 12 '26
This hit close to home 😅 The "You Might Not Need an Effect" docs page should honestly be mandatory reading before anyone writes their first useEffect.
A similar trap I finally escaped: storing derived data in useState. I used to do something like:
const [filteredList, setFilteredList] = useState([]);
useEffect(() => { setFilteredList(list.filter(item => item.active)); }, [list]);
When you can literally just write:
const filteredList = list.filter(item => item.active);
right in the render. No state, no effect, no stale data bugs. React re-runs the render anyway...just let it compute what it needs. Once I internalized that, I deleted an embarrassing amount of code.
-3
u/Equivalent_Head_4803 Apr 30 '26
I don’t get why anyone chooses React for anything now days other than every job posting listing React. It’s just terrible to work with when projects get huge. Spend a week in Vue or Svelte and you’ll never look back.
147
u/[deleted] Apr 30 '26
[removed] — view removed comment
13
u/[deleted] Apr 30 '26
[removed] — view removed comment