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.

Possible to support object keys?

See original GitHub issue

Would it be possible to support objects as keys rather than forcing everything to be a string?

In my case I’m looking to do something like

const { data } = useSwr(userDetail.prepare({ id: 5 }));

which returns the same object given the same inputs. The object itself has a method on it that is called by the fetcher:

function fetcher(preparedAction) {
    return preparedAction.call();
}

As it is this doesn’t work because the object returned is forced to a string here: https://github.com/zeit/swr/blob/master/src/use-swr.ts#L66

Given the cache is a Map using an object as a key works, eg. changing that line to the following appears to do what I want:

else if (!key || typeof key !== 'object') {
        // convert null to ''
        key = String(key || '');
}

Wrapping it in an array does also work:

const { data } = useSwr([userDetail.prepare({ id: 5 })]);

but the ergonomics aren’t as good (especially given if you forget to wrap it in an array it just doesn’t work - no errors).

Unfortunately other things assume it’s a string as well - eg. CONCURRENT_PROMISES etc are just plain objects rather than a Map.

Is this something that you would be willing to extend the API to accomodate?

My alternative to make this work within the current API constraints would be to have prepare return a string that fetcher can look up into my own global cache… but I was thinking it would be nice to avoid this if swr already did that internally.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:32 (20 by maintainers)

github_iconTop GitHub Comments

5reactions
Svishcommented, Dec 3, 2019

For simple cases, you could do something along the lines of what I did for Axios here: https://github.com/zeit/swr/tree/master/examples/axios

It basically just wraps useSWR in a new hook which uses JSON.stringify to create a key for the request object. Works fine so far here at least 🙂

3reactions
shudingcommented, Sep 6, 2021

Update: I’m planning to add key serialization and object key support in #1429.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"object.keys" | Can I use... Support tables for HTML5, CSS3, etc
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.
Read more >
Object.keys() - JavaScript - MDN Web Docs
The Object.keys() method returns an array of a given object's own enumerable string-keyed property names. Try it.
Read more >
JavaScript Object.keys() Function - GeeksforGeeks
JavaScript Object.keys() function is used to return an array whose elements are strings corresponding to the enumerable properties found ...
Read more >
Object.keys, values, entries - The Modern JavaScript Tutorial
They are supported for: Map; Set; Array. Plain objects also support similar methods, but the syntax is a bit different.
Read more >
JavaScript Object.keys() Method - W3Schools
Object.keys() is an ECMAScript6 (ES6) feature. ... Object.keys() is not supported in Internet Explorer 11 (or earlier). Related Pages. JavaScript Objects.
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