Post

Yeah, folks are getting from something that state setting in a useEffect is bad, period. So I end up getting even when it's something like this. I certainly agree sync setting is bad, but data loading like this is fine, IMHO. RQ is certainly nicer, but not required.

0 reposts11 likes

But honestly, I think this is a bad pattern anyway. Loading data in a React component that will render it is bad for the end user because you have to show them a loading state. It's more buttery smooth to find clever moments to prefetch and only use React once you have the data you need.

I think calling this a bad pattern (the promise based one, not the lint rule) maybe goes too far. There are some times where this actually makes the most sense, and we don’t do anyone any favors by calling it “bad”. We need to help developers build nuanced judgment, IMO

Hmm, not sure about that. I think we get told that "this is the new way of doing things" and as developers we adopt these new patterns blindly without paying attention to the user experience. I do think this pattern is bad, based on experience, and it's ok for devs to voice disagreement with norms.

Technical communication is hard because technically that set state is not “in” the effect. The effect just calls the promise. But you can’t blame a lot of people for thinking this *is* inside the effect since the code is between the brackets haha.

No idea where this is coming from. Setting state unconditionally during the synchronous portion of useEffect is bad, for the same reason that setting state unconditionally during render is bad (infinite render loop). Setting state asynchronously eg due to timer/subscription is fine.

An async effect is generally a really bad idea though. Assuming it had actual deps, as it would for a real use-case, you’d easily end up w race conditions. Earlier promises can resolve later. You’re really better off using a library like react-query, Relay, etc.

While I agree about the async effect being a bad idea, I think you can simply use an AbortController and trigger that in the clean up function of the effect. The neat thing is you can use that controller / signal for more than just fetch.

Yes, I absolutely agree. What I’m saying is that many developers struggle to understand the difference. It’s a reasonable mistake to make. It “looks” like the code is “inside” the effect. The lines literally are. We should do better helping people understand the fundamentals.