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.

Multiple Arguments with object in render breaks cache

See original GitHub issue

Hi,

It seems that multiple arguments with object in render breaks cache :

// Make sure objects are stable
const params = useMemo(() => ({ id }), [id])
useSWR(['/api/user', params], query)

If you follow this use case : id = 1 --> id = 2 --> id = 1 The second id = 1 doesn’t have the same key in cache as the first (because params doesn’t have the same ref)

Reproductible codesandbox : https://codesandbox.io/s/swr-multiple-arguments-no-cache-mfssd

The only way I found to preserve cache is to stringify in memo and parse in fetcher (https://codesandbox.io/s/swr-multiple-arguments-cache-p8iwy)

const params = useMemo(() => JSON.stringify({ id }), [id])

any better idea ?

Thanks !

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
shudingcommented, Sep 16, 2021

🎉 Added support for key serialization in #1429 and released as 1.1.0-beta.0, you don’t need useMemo anymore. CC: @nandorojo

3reactions
shudingcommented, Dec 1, 2019

Yes this is indeed a problem. In our real world use cases, we’re using it like this:

const { data: user } = useSWR('/api/user', fetch)
const { data: userItems } = useSWR(['/api/items', user], fetchWithUser)

Because user is a globally shared object, the cache key of the second query will always be stable. For use cases like query params, I will still suggest passing primitive values as args directly (will update documentation as well):

useSWR(['/api/user', id], (url, id) => query(url, { id }))

We have some discussions here https://github.com/zeit/swr/pull/145#discussion_r350064725.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple Arguments with object in render breaks cache #150
Hi, It seems that multiple arguments with object in render breaks cache : // Make sure objects are stable const params = useMemo(()...
Read more >
Best way to cache results of method with multiple parameters
var cacheKey:Object = { identifier:identifier, type:type, someBoolean:someBoolean }; //if key already exists, return it (not working) if ( ...
Read more >
Reading and writing data to the cache - Apollo GraphQL Docs
Apollo Client supports multiple strategies for interacting with cached data: ... Provide any required variables in this object. ... Let's break this down:....
Read more >
Arguments - SWR
SWR is a React Hooks library for data fetching. SWR first returns the data from cache (stale), then sends the fetch request (revalidate),...
Read more >
Flask-Caching — Flask-Caching 1.0.0 documentation
@app.route("/") @cache.cached(timeout=50) def index(): return render_template('index.html'). The cached decorator has another optional argument called ...
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