Best practice for guaranteeing query refresh on new page loads #1880
-
Hi folks 👋 , Wondering what the best practice would be in this case. Use case and /foo renders 10 components all of which use a recoil value where the underlying value is fetched from an api. When I switch between /bar and /foo, I want to guarantee that the page(/foo) makes a fresh api call to get the latest data. Approaches
What is the best practice for this use case? Really appreciate any advice 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I ended up going with something like this;
and have my recoil states that I want to be refreshed be dependent on this Using a component to leverage the |
Beta Was this translation helpful? Give feedback.
-
I think both approaches are completely valid and they both come with tradeoffs. The nice thing about the approach you chose is that the selector has the power of opting in/out of listening to route changes in the same location where the selector is defined, as opposed to the alternative of centralizing route-change effects to a separate hook higher in the tree (which is not a very Recoily thing to do since Recoil promotes decentralization). The only downside I see is that it feels awkward to subscribe to data for the sole purpose of busting the cache, since the data isn't actually used in the selector logic. At some point I think we should continue exploring the idea of Selector Effects so we could do things like:
But as far as I know Selector Effects aren't officially on the roadmap yet |
Beta Was this translation helpful? Give feedback.
-
Thanks for the 👀 Christian. Selector effects would be awesome. It would definitely help our use case. We'll stick with this implementation for now. |
Beta Was this translation helpful? Give feedback.
I ended up going with something like this;
and have my recoil states that I want to be refreshed be dependent on this
Using a component to leverage the
use…