If you are totally against setting state in a useEffect, how would you recommend doing this kind of thing? Assume a SPA.
3 reposts63 likes
If you are totally against setting state in a useEffect, how would you recommend doing this kind of thing? Assume a SPA.
3 reposts63 likes
I'm using TanStack Query. It can be handy if you have several pages with operations like this. Also it has an internal cache which is cool in the context of an SPA. tanstack.com/query/latest...
Depending on the scenario, it can either trigger infinite render loops: react.dev/reference/re... Or just a handful of unnecessary renders/renders with stale state: react.dev/learn/you-mi...
That’s fine for a simple single request or simple PoCs, you don’t need to over complicate things. However in a real world projects, I like TanStack Query tanstack.com/query/latest or SWR swr.vercel.app as a lightweight alternative.
Honestly just use @tanstack.com react-query's useQuery, it's so much cleaner for client side fetching. Or there's always the async RSC route.
The lint rule to avoid direct setState in useEffect allows this pattern: eslint-react.xyz/docs/rules/h...