Skip to content

useId函数

useld是一个React Hook,可以生成传递给无障碍属性的唯一ID

1. 使用useID

jsx
import { useId} from 'react'

function MyInput() {
  const passwordId = useId()
  return (
    <>
      <label>密码:
        <input type="password" aria-describedby={passwordId} />
      </label>
      <p id={passwordId}>密码不少于8位</p>
    </>
  )
}

function App() {
  return (
    <div>
      hello App
      <br />
      <MyInput />
    </div>
  )
}
export default App

Alt text