DEV Community

Discussion on: Share a code snippet in any programming language and describe what's going on

Collapse
 
jamesthomson profile image
James Thomson • Edited

Came up with this little snippet for Vue a couple years back that will assign a uid as a key on initial render and reuse the same key for subsequent renders. It's nothing fancy, but it's sure helpful.

function uid (el) {
  if (el.uid) return e.uid;

  // Could be replaced with actual uuid from lib
  const key = Math.random().toString(16).slice(2);

  Vue.set(el, 'uid', key);

  return el.uid;
}
Enter fullscreen mode Exit fullscreen mode