• [React] Use React ref to Get a Reference to Specific Components


    When you are using React components you need to be able to access specific references to individual component instances. This is done by defining a ref. This lesson will introduce us to some of the nuances when using ref.

    <input
              ref="b"
              type="text"
              onChange={this.update.bind(this)}
              />

    The way to refer to the 'ref':

    this.refs.b.value

    Also 'ref' is able to receive a callback function:

    <Input
              ref={ component => this.a = component}
              update={this.update.bind(this)}
            />
    
    
    class Input extends React.Component {
      render(){
        return <div><input ref="input" type="text" onChange={this.props.update}/></div>
      }
    }

    Now the way to access the ref value:

    this.a.refs.input.value,

    class App extends React.Component {
      constructor(){
        super();
        this.state = {a: '', b: ''}
      }
      update(){
        this.setState({
          a: this.a.refs.input.value,
          b: this.refs.b.value
        })
      }
      render(){
        return (
          <div>
            <Input
              ref={ component => this.a = component}
              update={this.update.bind(this)}
            /> {this.state.a}
            <hr />
            <input
              ref="b"
              type="text"
              onChange={this.update.bind(this)}
              /> {this.state.b}
          </div>
        )
      }
    }
    
    class Input extends React.Component {
      render(){
        return <div><input ref="input" type="text" onChange={this.props.update}/></div>
      }
    }
    
    ReactDOM.render(
      <App />,
      document.getElementById('root')
    );
  • 相关阅读:
    H3C日志文件读取
    sql2000 转sql2008
    常用sql大全
    安装linux后,重新装windows,修复mbr引导
    SQL Server推荐使用 SET 而不是 SELECT 对变量进行赋值
    ORCLE 截取固定字符
    又是一个无聊的周未
    转一个无聊的爱情故事:如果有个女生为你哭
    Windows Mobile 6 SDK
    扩展FCKeditor
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6371626.html
Copyright © 2020-2023  润新知