Skip to content

Refs

jsx
import { Component, createRef } from 'react'

class View extends Component {
  inputRef = createRef()

  handleClick = () => {
    console.log(this.inputRef)
    this.inputRef.current?.focus()
  }

  render() {
    return (
      <div>
        <input ref={this.inputRef} />
        <button type="button" onClick={this.handleClick}>
          聚焦
        </button>
      </div>
    )
  }
}

export default View

基于 MIT 许可发布