This is great. I’m going to be sharing this with my team tomorrow. I’ve been trying to explain some of this, but didn’t have great sources to back up what I was saying.
But I just learned some new things by reading this myself. I had no idea it was considered “ok” (in rare situations) to call a state setter right inside of a component function (as long as it’s nested in a condition). I would have used an effect for this case myself.
Yeah, that part caught me as well. They did show a solution after which required neither, but had I seen the inline state setter in a code review, I would have flagged it.
So did I. We're still finding old, unnecessary useEffects in our code base. Most of the team has fortunately understood when it's possible to avoid them, so we're catching this is code reviews now, and slowly weeding the remaining ones out.
I‘m rebuilding our funnel right now which used useEffect in every. single. component, multiple times. When I saw this page of the docs I was fully convinced to do it better. Really love the new docs.
156
u/janaagaard Mar 16 '23
I found that the "You Might Not Need an Effect" page was a really helpful resource in understanding how React works.
https://react.dev/learn/you-might-not-need-an-effect