Repozytorium Web Developera

React Refs

Useful links

Get reference to your component

You can get a reference to an element by using ref property. When you use class component you can easily set a reference to choosen element by setting on it the prop ref={el => { this.el = el }} and then you will be able to access it properties, for example in component lifecycle methods to focus it by use this.el.focus().


class Input extends Component {
  focus () {
    this.el.focus()
  }
  
  render () {
    return (
      <input
        ref={el => { this.el = el }}
      />
    )
  }
}