How to refresh/invalidate an asynchronous selector?
See original GitHub issueSelectors can be used to asynchronously fetch data from an API. But how is it possible to trigger a re-fetch of this data?
Given this selector:
const todosState = selector({
key: "todosState",
get: async () => {
const result = await fetch("https://example.com/todos");
const todos = await result.json();
return todos;
},
});
In a scenario where a user wanted to reload his todos, because he knows his coworker added a new todo to the list. How would he trigger this selector to re-fetch the todos from the API?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:14
- Comments:27 (7 by maintainers)
Top Results From Across the Web
How to force browsers to reload cached CSS and JS files?
And if you're using Chrome as your development environment, another non-invasive solution is to disable the cache: Under the Settings cog, you can...
Read more >useRecoilRefresher_UNSTABLE(state)
The useRecoilRefresher_UNSTABLE() hook returns a callback which can be called with a selector to clear any caches associated with it.
Read more >RTK Query: The future of data fetching and caching for Redux
Their caching strategies are quite different as well: RTK Query is declarative in data invalidation, and its cache key is created via endpoints ......
Read more >Application Data Caching
This method can be used to force a refresh of the cache entry corresponding to the given key. 2, This method will invalidate...
Read more >modifying a row not getting updated to DB - row.invalidate()
I am trying to make a button that changes the value of a particular field ("status") for every selected row, and update to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@acutmore Thanks for your suggestion. Although your code works as intended, it feels more like a workaround instead of an official solution. I genuinely thought this update could be triggered using the
useResetRecoilState()
function.An obvious way to make this less of a workaround would be to simply use an atom and handle the update manually. However, I really like the concept of the Data Flow Graph mentioned in the doc, which makes data fetching really declarative and implicit for the consumer of the selector.
How about adding a re-evaluate feature to the API, either with
useResetRecoilState()
or with a new function in the selector options?Hi @philippta. I agree, does feels more like a workaround. Maybe someone from the Recoil team will have a more official solution.
I had another go using
useResetRecoilState
. Callingreset
sets an Atom’s value back to thedefault: ...
value in the Atom’s config, or calls a selector’sset
passing in Recoil’sDefaultValue
. I had a go and selectors do not seem to support async set. So this gets back to synchronously updating an atom to force a selector’s async get to re-run.