amplitude / redux-query

A library for managing network state in Redux
https://amplitude.github.io/redux-query
Other
1.1k stars 67 forks source link

Question about where to put side effects #203

Closed TheCarlR closed 1 year ago

TheCarlR commented 4 years ago

If there's a need to store a single identifier in for instance localstorage, based on the content of some api call managed by redux-query. Where would you suggest to handle that? Is there any part in redux query that's suitable? Is redux middleware better/worse? Handle manually outside useMutation and useSelector?

The identifier in this case would be fetched from part of an api answer the very first time accessing the app, and then stored in localstorage. For all subsequent times it would be fetched from localstorage and be used in the arguments to the api calls.

rctbusk commented 4 years ago

Hmmm, question why this storing in localstorage is important? Do you need it to persist across every session?

I ask this because if you are making the call at the start of each session, why not just store this in the redux store for that session.

TheCarlR commented 4 years ago

We need it to restore the last session from API. :)

On Mon, Sep 21, 2020 at 10:18 PM rctbusk notifications@github.com wrote:

Hmmm, question why this storing in localstorage is important? Do you need it to persist across every session?

I ask this because if you are making the call at the start of each session, why not just store this in the redux store for that session.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/amplitude/redux-query/issues/203#issuecomment-696349750, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFAMKLC7WOOKYKAVKFBXBZ3SG6YIBANCNFSM4RQFNWSQ .

rctbusk commented 4 years ago

ahhhh gotcha

If this can happen with any network call coming through redux-query, I would say adding a middleware is probably the easiest.

If it is one specific network call that does this, I would handle outside of useMutation, as to not add unnecessary steps that only one call uses.

TheCarlR commented 4 years ago

If I would go with a middleware, is it possible to filter so I only react on updates on a specific entity?

On Mon, Sep 21, 2020 at 10:24 PM rctbusk notifications@github.com wrote:

ahhhh gotcha

If this can happen with any network call coming through redux-query, I would say adding a middleware is probably the easiest.

If it is one specific network call that does this, I would handle outside of useMutation, as to not add unnecessary steps that only one call uses.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/amplitude/redux-query/issues/203#issuecomment-696353600, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFAMKLCHZ4OJ2WQLPKHMS43SG6ZA7ANCNFSM4RQFNWSQ .

rctbusk commented 4 years ago

Hmmm. Probably not. You could have it check to see if the value stored in redux-query matches the one in localstorage, and if it doesn't match, then update the localstorage value. Sorry it is a bit hacky.