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.

Cache miss when transforming value with useMemo()

See original GitHub issue

Bug report

Description / Observed Behavior

I am using dependant SWR requests. The first one is fetching an array of users, the second one is fetching additional information for each individual user. In order to reduce the amount of requests I filter the array of user for only active ones using useMemo().

Although the resulting array of filtered user is exactly the same between page transition, it results in a cache miss (which means it doesn’t return the cached value, instead waiting for the async request to complete).

Expected Behavior

I expect SWR to return stale data first when I use exactly the same key + data input between page transitions.

Repro Steps / Code Example

I created a Code Sandbox to reproduce the behaviour. CodeSandbox

Additional Context

My understanding is, that for dedouping we use dequal, which does a deep comparisons. I have found some information about how we evaluate, if the cache can provide stale data. The key is essential, but also the dependent data passed alongside the key is relevant. Here I expected also a deep comparison, but according to my example it doesn’t seem to be the case.

Am I missing something?

SWR 0.5.6 React 17.0.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
nataliemarlenycommented, Aug 23, 2021

@gonzochic Using JSON.stringify and JSON.parse seems like a reasonable approach to me with some caveats:

  1. the object should be serializable of course,
  2. JSON.stringify doesn’t guarantee the order of the keys in the string, so it could be better to use something like json-stable-stringify.

I’ve looked through the docs to see if there’s a general answer for how to handle this, but I haven’t found one. I remember reading in some issues that the recommended approach is to spread the nested array into the array that represents the useSWR key. Though, there are limitations to this approach as well.

0reactions
shudingcommented, Sep 16, 2021

Closing since key serialization was added in #1429 and released as 1.1.0-beta.0, you don’t need useMemo anymore! 🎉

Read more comments on GitHub >

github_iconTop Results From Across the Web

React useMemo is not caching results - Stack Overflow
I have simple React app in Typescript and I am trying to utilize useMemo() , but is not working. useMemo , if I...
Read more >
React.memo() vs. useMemo(): Major differences and use cases
In simple terms, memoization is a process that allows us to cache the values of recursive/expensive function calls so that the next time...
Read more >
Cache Computed Values for Better Performance ... - Egghead.io
In this lesson, we'll use useMemo to memoize (cache) a label that is computed when the user presses a button - but we'll...
Read more >
useMemo / useCallback cache busting opt out #15278 - GitHub
According to the React docs, useMemo and useCallback are subject to cache purging: You may rely on useMemo as a performance optimization, ...
Read more >
useMemo - React Docs
useMemo is a React Hook that lets you cache the result of a calculation between re-renders. const cachedValue = useMemo(calculateValue, dependencies).
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