Saturday, September 21, 2019

React: Using Ref

class Person extends Component {
    constructor(props) {
        super(props);
        this.inputElementRef = React.createRef();
    }

}

componentDidMount() {
    this.inputElement.current.focus();
    //old approach
    // this.inputElement.focus();
}

render() {
    ...
    <input
      key="k1"
      // old approach
     // ref={(inputEl)=>{this.inputEleemnt=inputEl}}
      ref={this.inputElementRef}
      type="text"
    ...
}

No comments:

Post a Comment