question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to refresh/invalidate an asynchronous selector?

See original GitHub issue

Selectors 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:closed
  • Created 3 years ago
  • Reactions:14
  • Comments:27 (7 by maintainers)

github_iconTop GitHub Comments

24reactions
philipptacommented, May 18, 2020

@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?

21reactions
acutmorecommented, May 18, 2020

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. Calling reset sets an Atom’s value back to the default: ... value in the Atom’s config, or calls a selector’s set passing in Recoil’s DefaultValue. 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.

const todosTrigger = atom({
  key: "todosTrigger",
  default: 0
});

const todosSelector = selector({
  key: "todosSelector",
  get: async ({ get }) => {
    get(todosTrigger);
    return await getTodos();
  },
  set: ({ set }, value) => {
    if (value instanceof Recoil.DefaultValue) {
      set(todosTrigger, v => v + 1);
    }
  }
});

function Todos() {
  const todos = useRecoilValue(todosSelector);
  const reset = useResetRecoilState(todosSelector);
  console.log(todos);
  return <button onClick={() => reset()}>Reset</button>;
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found