Understanding How to Use the useId Hook in React 18

Understanding How to Use the useId Hook in React 18

Dos and Don't when using useId in React
Ferenc Almasi β€’ 2022 June 14 β€’ Read time 3 min read
  • twitter
  • facebook
React

In React 18, several new hooks have been introduced, among many, the useId hook which can be used for generating unique ids inside your components. To generate a unique ID, you can do the following:

Copied to clipboard! Playground
import React, { useId } from 'react'

const Hooks = () => {
    const id = useId() // Generates a random ID as a string, eg ":r1:"

    return (
        <React.Fragment>
            <label htmlFor={id}>Check me</label>
            <input type="checkbox" id={id} name="hook" />
        </React.Fragment>
    )
}

They are meant to be stable between the server and client-side, to avoid mismatches during hydration. It generates a string that includes a colon and is unique. This means you should avoid using it together with CSS.

They are also not meant to be used for unique keys in a list. For most cases, you can use the index of your array instead, or an ID returned from your database:

Copied to clipboard! Playground
// πŸ”΄ Don't use useId for keys
const id = useId()

return posts.map(post => (
    <article key={id}>
        ...
    </article>
))

// 🟒 Do use indexes
return posts.map((post, index) => (
    <article key={index}>
        ...
    </article>
))

// 🟒 Do use ids returned from database
return posts.map(post => (
    <article key={post.id}>
        ...
    </article>
))

You can also use a single generate ID with multiple elements by simply prefixing or suffixing them as needed in the following way:

Copied to clipboard! Playground
import React, { useId } from 'react'

const Hooks = () => {
    const id = useId()

    return (
        <React.Fragment>
            <label htmlFor={`email-${id}`}>Email</label>
            <input type="text" id={`email-${id}`} name="email" />

            <label htmlFor={`password-${id}`}>Password</label>
            <input type="password" id={`password-${id}`} name="password" />
        </React.Fragment>
    )
}

When should you use the useId hook?

  • Use it for connecting two HTML elements together such as a label with an input
  • Use it for places where you need a unique ID to be generated every time
  • Use prefix or suffix for using the same generated ID over multiple times

When should you not use the useId hook?

  • Do not use the useId hook for CSS selectors
  • Do not use it for keys in a list
  • Do not use it for values that should not change

Just like any other React hook, useId can only be used in functional components, or a custom React hook function.

Try the hook on codesandbox
Understanding How to Use the useId Hook in React 18
If you would like to see more Webtips, follow @flowforfrank

If you are interested in reading more about React hooks, seeing more examples for custom hooks, or just learning about the basics, make sure to check out the article below.

All You Need to Know About React Hooks
  • twitter
  • facebook
React
Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Access 100+ interactive lessons
  • check Unlimited access to hundreds of tutorials
  • check Prepare for technical interviews
Become a Pro

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.