Multiple Arguments with object in render breaks cache
See original GitHub issueHi,
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:
- Created 4 years ago
- Reactions:3
- Comments:16 (5 by maintainers)
Top 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 >
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 Free
Top 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
🎉 Added support for key serialization in #1429 and released as
1.1.0-beta.0
, you don’t needuseMemo
anymore. CC: @nandorojoYes this is indeed a problem. In our real world use cases, we’re using it like this:
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):We have some discussions here https://github.com/zeit/swr/pull/145#discussion_r350064725.