-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add meta as argument for key filter #2161
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 9d6086a:
|
5a6fa0f
to
5268d85
Compare
My 2 cents: let's keep this on hold (I'm sorry!). The serialization process (under the hood called "stable hash") actually doesn't guarantee the result. That's also why we still expose it as So I'm wondering if matching the hashed string is a good idea. Your example is actually the case: Some more examples: useSWR({ id: 'foo' })
// ' will become " after serialization useSWR(['/api/1', opts])
// opts might have any arbitrary content inside How do you match these using the serialized key? The only conclusion I had is to never match serialized keys but match the original key, although it's more complicated. |
Make sense, let's close it for now! And encourage users to use the accurate matching or compare when mutate multiple keys while multiple shapes of keys existing. Encouraged ✔️// matching array key
mutate((key) => key[0].startsWith('/api'), data)
// matching string key
mutate((key) => typeof key === 'string' && key.startsWith('/api'), data) Discouraged ❌
|
@huozhi - mutate((key) => key[0].startsWith('/api'), data)
+ mutate((key) => Array.isArray(key) && key[0].startsWith('/api'), data) |
@koba04 Yeah but only if you're using array key. The first 2 cases I posted are both the ways to go |
If the keys are non string, using filter will need to
serialize
the original keys to string, and do regext matching or string compare, adding a new argument meta for it, includingserializedKey: string
property for the filter to let user easily match the keySo that you can match the original key, or use regext to match the
meta.serializedKey
In the future we can extend the meta with more info to match the keys more flexibily