react中ref的使用

在react中获取真实dom的时候就需要用到ref属性,具体使用如下

var MyComponent = React.createClass({
  handleClick: function() {
    console.log(this.input)
  },
  render: function() {
    return (
      <div>
        <input type="text" ref={(input) => {this.input = input}} />
        <input type="button" value="Focus the text input" onClick={this.handleClick} />
      </div>
    );
  }
});

ReactDOM.render(
  <MyComponent />,
  document.getElementById('example')
);
原文地址:https://www.cnblogs.com/lanshu123/p/10628521.html