-
-
Notifications
You must be signed in to change notification settings - Fork 5k
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(nuxt): useId
composable
#23368
feat(nuxt): useId
composable
#23368
Conversation
Run & review this pull request in StackBlitz Codeflow. |
✅ Live Preview ready!
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice idea! and relevant for #23255.
useId
composablegetUniqueID
utility
You can try adding a local ID const nuxt = useNuxtApp()
const localId = (nuxt.payload?.localIds?.[key] ?? 0) + 1
nuxt.payload.localIds = nuxt.payload.localIds ?? {}
nuxt.payload.localIds[key] = localId Then repeatedly calling the component will not cause conficts <script setup lang="ts">
import {getUniqueID} from '#imports'
withDefaults(defineProps<{ id?: string }>(), {
id: () => getUniqueID()
});
</script>
<template>
<label :for="id">Label</label>
<br>
<input type="text" :id="id" :placeholder="id"/>
</template>
|
Given that the main use case for this utility is to generate unique
Here's a blog post that has some useful ideas and links: https://www.jovidecroock.com/blog/preact-use-id |
Those are both good suggestions. A hybrid approach that gets a unique constant and uses it as prefix + an additional per-instance value seems good to me. Marking as draft for now - do feel free to change that whenever it's good to go 🙏 |
🚀 Release triggered! You can now install nuxt@npm:nuxt-nightly@pr-23368 |
I have given it a try locally (while also fixing some edge cases that are present in its current state), but I was unable to find a way to make Preact's solution work in SSR. On the client, it is possible to locate a certain vnode's path by traversing up the tree until you reach the root, storing each node's index on its parent's children. But on the server, the If we are able to locate the vnode's path in the tree on the server, or have some unique identifier to each instance that remains stable after hydration, then this composable could be used in any component without having to store the ids in the root element's attributes (it could go directly to the payload as a I'm not very well-versed in either Nuxt or Vue internals, so please tell me if I've missed something. |
As a note - this is planned for Vue 3.5 to be supported natively. Source |
Oh that is very good news, thanks for sharing. Should be much more reliable this way as Vue handles the vnode tree. |
Exactly, I believe it should come from Vue core |
Does that mean that this isn't coming to Nuxt and will be entirely implemented in Vue core? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can go ahead and merge this for now, and swap across to the Vue native implementation when it comes (and iterate to fix any bugs). For now, there's nothing Nuxt-specific about this implementation other than the automatic provision of a key that is unique per file + line number - and all data is transferred from server -> client via data attributes.
I get the following error when trying to use |
Try removing |
@danielroe fixed it! Thanks a lot. |
@DJafari i am actually having the same problem. |
If you have an issue, please open a new issue with a reproduction rather than commenting on this PR 🙏 |
@danielroe Because I cannot recreate this problem in the new project, I am commenting here but it's has a bug, in some case i use |
@DJafari no matter if you can reproduce it or not, commenting on a merged PR is not the place to report bugs. That should be an own issue. If you want reproduce the issue the problem might be part of your code then? |
Co-authored-by: Daniel Roe <daniel@roe.dev> Co-authored-by: Sébastien Chopin <seb@nuxt.com>
🔗 Linked issue
❓ Type of change
📚 Description
The
useId
composable creates a unique id.📝 Checklist