点击按钮focus到某个输入框

import React, { useRef, useEffect } from 'react'

export default function TextInputWithFocusButton() {
  const inputEl = useRef(null)
  const onButtonClick = () => {
    // `current` 指向已挂载到 DOM 上的文本输入元素
    inputEl.current.focus()
  }

  useEffect(() => {
    inputEl.current.focus()
  }, [])
  return (
    <>
      <input ref={inputEl} type="text" />
      <button onClick={onButtonClick}>Focus the input</button>
    </>
  )
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2023-03-19
  • 2022-12-23
猜你喜欢
  • 2021-09-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2023-01-28
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案