react中控制节点

class App extends React.Component {
  handleClick = () => {
    const { input } = this.inputRef; // 如果是textArea的话,const { textAreaRef } = this.inputRef;
    input.focus();
    input.setSelectionRange(0, input.value.length);
    // input.select(); // 可全部选中
  };
  
  render() {
    return (
      <div>
        <Input ref={(input) => { this.inputRef = input; }} defaultValue="foo" />
        <button onClick={this.handleClick}>Focus</button>
      </div>
    );
  }
}

 

posted @ 2019-10-12 14:39  陈小作  阅读(366)  评论(0编辑  收藏  举报