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.

useUid - generate unique id for a Vue instance

See original GitHub issue

When authoring accessible components, sometimes you need an unique id to use on eg. <label for=""/> elements or for aria-describedby attrs

<template>
  <div id="description">This is a deadly {{animal}}</div>
  <span aria-describedby="dsecription">{{ animal ==== 'snake' ? 'sssssssssss' : 'bark?'  }}</span> 
</template>

The above example is wrong because what if you use the above component many times in the same wrapper component? The id=“description” occurs several times and is not uniquely tied to one single, correct description

That can be fixed by doing :id=“uid”, where uid is the uniqe id generated during created() lifecycle hook

Propsal:

<script setup>
const {uid} = useUid();
</script>

I think it’s worth providing it as a composable that you have to destructure rather than a raw function to prevent people from calling uuid() manually in the template (which I guess could generate a different id on rerenders, especially if you used uuid() multiple times in the template, with const {uuid} = useUuid() it’s harder to run into that problem)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
bklikcommented, Jun 22, 2022

Came here kind of looking for the same thing. Here’s another option:

// Returns a random Universally Unique Identifier (UUID)
export function useUUID(pattern?: string) {
    // Accept any desired pattern. If no pattern is provided
    // default to a RFC4122 UUID pattern.
    const _pattern = pattern ? pattern : 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';

    // Replace each character in the pattern
    // leaving any non x|y character alone.
    return _pattern.replace(/[xy]/g, replacePattern);
}

function replacePattern(c: string) {
    // Random hexadecimal number
    const r = (Math.random() * 16) | 0;

    // If 'x' return hexadecimal number,
    // if 'y' return [8-11] randomly
    const v = c == 'x' ? r : (r & 0x3) | 0x8;
    return v.toString(16);
}
0reactions
iamandrewlucacommented, May 19, 2022

Also for who just wants a simple unique id, that does not repeat, and not need to be uuid

use-id.js

let id = 0;
export function useUid() {
  return String(id++)
}
<template>
  <label :for="firstId">Name</label>
  <input :id="firstId" type="text" />

  <label :for="secondId">Surname</label>
  <input :id="secondId" type="text" />
<template>
<script setup>
  import { useId } from "./use-id";

  let firstId = useId();
  let secondId = useId();
</script>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue.js: How to set a unique ID for each component instance?
The simplest way I found was to create a UUID ( uuid package ) manually through a global mixin.
Read more >
Vuejs Generate Unique Identifier UUID or GUID with example
In this blog post, We are going to learn how to generate UUID or GUID in the vuejs application with examples. You can...
Read more >
vue-uuid - npm
Add UUID to Vue instance.. Latest version: 3.0.0, last published: 10 months ago. Start using vue-uuid in your project by running `npm i ......
Read more >
Vue.js: How to set a unique ID for each component instance?
The simplest way I found was to create a UUID ( uuid package ) manually through a global mixin. That way you won't...
Read more >
Vue.js : How to set a unique ID for each component instance?
The Best Answer is ... Each component has a unique id which can be accessed as this._uid . ... If you want more...
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