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.
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