top | item 33506861

(no title)

isakkeyten | 3 years ago

Be careful. You will summon acemarke with this comment.

discuss

order

acemarke|3 years ago

HAH! and ironically I read this literally 10 minutes after you posted it :)

dons maintainer hat

<ObligatoryResponse>

We've been generally recommending against use of sagas for years - they're a power tool, and very few apps need that. They're also a bad fit for basic data fetching.

Today, Redux Toolkit's "RTK Query" API solves the data fetching and caching use case, and the RTK "listener" middleware solves the reactive logic use case with a simpler API, smaller bundle size, and better TS support.

Resources:

- https://redux.js.org/tutorials/essentials/part-7-rtk-query-b...

- https://redux-toolkit.js.org/rtk-query/overview

- https://redux-toolkit.js.org/api/createListenerMiddleware

- https://blog.isquaredsoftware.com/2022/06/presentations-mode...

</ObligatoryResponse>

whilenot-dev|3 years ago

I'll use your nice response for an actual question/remark, gotcha! =)

Recently I had a look at the kubeshop-dashboard repo[1] and their use of the RTK Query API[2]. When I write the boilerplate for any SPA nowadays, I usually like to merge any fetching logic with the lib-specific notification/toast-methods, in order to render something to the user about any reached warning- or error-timeouts for each ongoing fetch by default. Meaning:

- every new fetch would start a timer

- after 10secs a warning-notification is shown "a fetch takes longer than expected..."

- and after 30secs the AbortController signals the cancelling of the ongoing fetch and an error-notification is shown "fetch took to long. click to try again."

The implementation of react-query, its "hook-yfied" nature, makes it super easy to wrap it and merge it with the component-lib to create such a thing. I just need to wrap its provided hooks (useQuery, useMutation) with "hook-creators" (I usually call them createQueryHook and createMutationHook) and don't need to dive into any of its implementation specific details. But createApi, as provided by RTK Query API, makes this quite a bit harder, or so it seems to me at least. How would you wrap createApi to provide such a functionality for every fetch by default?

[1]: https://github.com/kubeshop/testkube-dashboard

[2]: https://github.com/kubeshop/testkube-dashboard/tree/main/src...

mcluck|3 years ago

What's weird is that I still firmly believe that sagas is one of the sanest ways of organizing an application. I built a sort of boilerplate project that shows how I use it[1] but the TL;DR is that I can wrap all of my functionality into nice little sagas and manage the state very easily with lenses. Handling data fetching isn't too complicated either [2] but I'm also not doing any sort of fancy caching in this example.

[1]: https://github.com/MCluck90/foal-ts-monorepo/blob/main/app/c...

[2]: https://github.com/MCluck90/foal-ts-monorepo/blob/main/app/c...