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.

Ability to clear selector caches

See original GitHub issue

Hi there, we have an app using v0.1.2 of Recoil and tried today to upgrade to v0.2.0 but ran into an issue. It seems that atom state is being shared across different instances of RecoilRoot which makes it difficult to test certain types of default values.

For example, we have an atomFamily that derives the default values based on URL params. In our tests, we set up the URL state and then access the atomFamily. With this caching we are now seeing, this means that only the first test works properly as the subsequent tests do not actually retrieve the value from the URL.

Here is a simplified reproduction for clarity.

I know in the documentation there is note about this:

Note that caches, such as selector caches, may be shared across roots.

But you’ll note in the reproduction above, that even using reset() doesn’t appear to invalidate the cache. (I also tried using key, but that yielded the same results.)

Is this intended behavior? If so, what would be the recommended approach to fixing this? Is there some method to clear the Recoil caches or force the RecoilRoot to not use the cache?

Thanks in advance for your help!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:10
  • Comments:25 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
trentmwilliscommented, Feb 17, 2022

It’s been a little bit since I followed up on this, but happy to report that with the new “refresh” API and this example in the docs, we were able to get our app upgraded to the latest version of Recoil and all tests fixed/passing.

Thanks to everyone that contributed to this!

3reactions
sebinsuacommented, Jun 16, 2022
const clearSelectorCachesState = selector({
  key: 'ClearSelectorCaches',
  get: ({getCallback}) => getCallback(({snapshot, refresh}) => () => {
    for (const node of snapshot.getNodes_UNSTABLE()) {
      refresh(node);
    }
  }),
});

const clearSelectorCaches = testingSnapshot.getLoadable(clearSelectorCachesState).getValue();

// Assuming we're in a file added to Jest's setupFilesAfterEnv:
afterEach(clearSelectorCaches);

@trentmwillis Where has testingSnapshot meant to come from in the example?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Recoil 0.4
The new cachePolicy_UNSTABLE property in selectors and selector families allows you to configure the caching behavior of a selector's internal ...
Read more >
How to Clear Cache for All Major Browsers - Kinsta
Find out how to clear the browser cache in most major browsers with these quick ... Before clearing the cache selection, click on...
Read more >
How Do I Fix My Caching Problems Or Clear Web Browser's ...
How to clear the cache in commonly used browsers · Open your browser and and got to Tools > Safety menu. · Click...
Read more >
Clear cache API | Elasticsearch Guide [8.5] | Elastic
Clear cache APIedit. Clears the caches of one or more indices. For data streams, the API clears the caches of the stream's backing...
Read more >
How to Clear Your Android Cache & Why You Should Do It
How to clear app cache on Android · Open Settings and tap Storage. · Choose if you want to clear the cache on...
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