2.21 useId生成唯一ID值

一、useId生成唯一ID值

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

  2. 示例:

    import { useId } from 'react'
    
    function MyInput() {
      const password = useId()
    
      return (
        <>
          <label>
            密码:
            <input type="password" aria-describedby={password} />
          </label>
          <p id={password}>密码应该包含至少16个字符</p>
        </>
      )
    }
    
    function App() {
      return (
        <>
          <div>Default Template</div>
          <br />
          <MyInput />
          <MyInput />
        </>
      )
    }
    
    export default App
    
  3. useDebugValue:在开发工具中,可以打印自定义内容,必须在自定义Hook中

  4. useSyncExternalStore:操作额外的store

  5. 自定义Hook,第三方Hooks库

最后更新于