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.

Generating unique ID's and SSR (for a11y and more)

See original GitHub issue

Howdy ya’ll,

tl dr: please provide a way to coordinate pseudo-random identifiers across the client and server

This issue has been discussed a bit before (#1137, #4000) but I continually run into this issue, trying to build libraries that provide accessible components by default. The react component model generally speaking offers a big opportunity to raise the often low bar for accessibility in the library and widget world, see experiments like @ryanflorence’s react-a11y.

For better or for worse the aria, and a11y API’s in the browser are heavily based on using ID’s to link components together. aria-labelledby, aria-describedby, aria-owns,aria-activedescendent, and so on all need’s ID’s. In a different world we would just generate ids where needed and move on, however server-side rendering makes that complicated, because any generated ID is going to cause a mismatch between client/server.

We’ve tried a few different approaches to address some of this, one is making id’s required props on components that need them. That gets kinda ugly in components that need a few id’s but moreso it annoys users. Its unfortunate because if we could generate deterministic id’s we could just provide more accessible components by default.

The frustrating part is that the component generally has all the information it needs to just set the various aria info necessary to make the component usable with a screen reader, but are stymied by not having the user provide a bunch of globally unique ids’

So far only really reasonable approaches I’ve seen are @syranide’s solution of a root ID store, and using _rootID. The latter obviously has problems. The former doesn’t scale well for library authors. Everyones’ root App component is already wrapped in a Router, Provider, etc, having every library use their own root level ID provider is probably not super feasible and annoying to users.

It seems like the best way to do this would be if React (or a React addon) could just provide a consistent first class way to get a unique identifier for a component, even if it is just a base64 of the node’s _rootID.

thanks for all the hard work everyone!

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:44
  • Comments:37 (19 by maintainers)

github_iconTop GitHub Comments

34reactions
gaearoncommented, Aug 21, 2020

Just to follow up on this, we are testing a potential solution to this internally but not ready to draft an RFC yet.

33reactions
milesjcommented, Jan 17, 2016

You’re over-thinking this a bit. All the reasons you keep stating are easily fixable. Assuming that the server and client render everything in the same order (not sure why they wouldn’t), just have a separate function module that generates and keeps track of things.

let idCounter = 0;

export default function generateUID(inst) {
    return btoa(inst.constructor.name) + idCounter++;
}

And in the component.

import generateUID from './generateUID';

class Foo extends React.Component {
    constructor() {
        super();
        this.uid = generateUID(this);
    }
}

And if for some reason you want individually incrementing counters per component, then just use a WeakMap in the function module.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React 18 provides useId API for generating unique IDs on ...
useOpaqueIdentifier API generates a unique ID based on whether the hook was called on the server or client. If the hook is called...
Read more >
Unique IDs and where to find them | by Anton Korzunov
It's impossible to manually generate unique IDs for all the elements and never clash. It's not easy to give an obvious “names”(ids) to...
Read more >
How to generate unique IDs for form labels in React?
I have form elements with label s and I want to have unique IDs to link label s to elements with htmlFor attribute....
Read more >
An easy way to name Labels and generate unique keys.
An easy way to name Labels and generate unique keys. A story about Accessible Accessibility, Keys, IDs, and Reconciliation.
Read more >
Generating unique IDs in a distributed environment at high scale
This approach uses a centralized database server to generate unique incrementing IDs. It's like a centralized auto-increment. This approach is ...
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