React中ref属性的使用

1、在ref使用的过程中,需要注意的是:

0.14.8版本及以前用 this.getDOMNode() 或者 this.findDOMNode(this.refs.tip)

15.0版本后用 ReactDOM.findDOMNode(this.refs.tip);

2、我们通过代码进行对ref的学习:如下面代码:

<html>
<head>
    <meta charset="utf-8">
    <title>学习React!!</title>
</head>
<body>
    <div id="app"></div>
    <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
    <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
    <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
    <script src="bower_components/react/react.js"></script>
    <script src="bower_components/react/react-dom.js"></script>
    <!--  注意下面注释掉的的方式是旧版本所使用的编译转换方式,没有注释的现在使用的-->
    <script src="bower_components/babel/browser.js"></script>
    <script type="text/babel">
      class FormApp extends React.Component {
          constructor(props) {
              super(props);
              this.handleSubmit = this.handleSubmit.bind(this);
              this.handleRadio= this.handleRadio.bind(this);
              this.handleCheck = this.handleCheck.bind(this);
              this.state = {
                  inputValue: 'input value',
                  selectValue: 'A',
                  radioValue: 'B',
                  checkValues: [],
                  textareaValue: 'some text here,,,,,'
              }
          }
          handleSubmit(e){
              e.preventDefault();
        /*    console.log(e);
              console.log(ReactDOM.findDOMNode(this.refs['goodInput']).value);
              console.log(ReactDOM.findDOMNode(this.refs['goodSelect']).value);
              console.log(ReactDOM.findDOMNode(this.refs['goodTextarea']).value);
        */
              var formData = {
                  input: ReactDOM.findDOMNode(this.refs['goodInput']).value,//this.refs.goodInput.findDOMNode().value,
                  select:ReactDOM.findDOMNode(this.refs['goodSelect']).value ,//this.refs.goodSelect.findDOMNode().value,
                  textarea:ReactDOM.findDOMNode(this.refs['goodTextarea']).value ,//this.refs.goodTextarea.findDOMNode()().value,
                  radio: this.state.radioValue,
                  check: this.state.checkValues,
              }
              console.log(formData);
          }
          handleRadio(e){
              this.setState({
                  radioValue: e.target.value,
              })
          }
          handleCheck(e){
              var checkValues = this.state.checkValues.slice();
              var newVal = e.target.value;
              var index = checkValues.indexOf(newVal);
              if( index == -1 ){
                  checkValues.push( newVal )
              }else{
                  checkValues.splice(index,1);
              }
              this.setState({
                  checkValues: checkValues,
              })
          }
          render(){
              return (
                      <form onSubmit={this.handleSubmit}>
                          <input ref="goodInput" type="text" defaultValue={this.state.inputValue }/>
                          <br/>
                          选项:
                          <select defaultValue={ this.state.selectValue } ref="goodSelect">
                              <option value="A">A</option>
                              <option value="B">B</option>
                              <option value="C">C</option>
                              <option value="D">D</option>
                              <option value="E">E</option>
                          </select>
                          <br/>
                          <p>radio button!</p>
                          <RadioButtons ref="goodRadio" handleRadio={this.handleRadio} />
                          <br />
                          <Checkboxes handleCheck={this.handleCheck} />
                          <br />
                          <textarea defaultValue={this.state.textareaValue} ref="goodTextarea"></textarea>
                          <br />
                          <button type="submit">提交</button>
                      </form>
              )
          }
      }
      class RadioButtons extends React.Component{
          saySomething(){
              alert("yo what's up man!!!");
          }
          render(){
              return (
                      <span>
            						A<input onChange={this.props.handleRadio} name="goodRadio" type="radio" value="A"/>
            						B<input onChange={this.props.handleRadio} name="goodRadio" type="radio" defaultChecked value="B"/>
            						C<input onChange={this.props.handleRadio} name="goodRadio" type="radio" value="C"/>
            					</span>
              )
          }
      }
      class Checkboxes extends React.Component{
          render(){
              return (
                    <span>
                        A<input onChange={this.props.handleCheck}  name="goodCheckbox" type="checkbox" value="A"/>
                        B<input onChange={this.props.handleCheck} name="goodCheckbox" type="checkbox" value="B" />
                        C<input onChange={this.props.handleCheck} name="goodCheckbox" type="checkbox" value="C" />
		                </span>
              )
          }

      }

		var formApp = ReactDOM.render(
			<FormApp />,
			document.getElementById('app')
		)
	</script>
</body>
</html>

在上面代码中,我们通过下面这段代码进行值得获取,使用的主要方法有findDOMNode(),其中this.refs['goodInput']中的goodInput为标签的name

              var formData = {
                  input: ReactDOM.findDOMNode(this.refs['goodInput']).value,//this.refs.goodInput.findDOMNode().value,
                  select:ReactDOM.findDOMNode(this.refs['goodSelect']).value ,//this.refs.goodSelect.findDOMNode().value,
                  textarea:ReactDOM.findDOMNode(this.refs['goodTextarea']).value ,//this.refs.goodTextarea.findDOMNode()().value,
                  radio: this.state.radioValue,
                  check: this.state.checkValues,
              }

为了能够使用上面的代码,我们进行了如下修改,在原来的代码中加入ref标签

代码如下所示:

<input ref="goodInput" type="text" defaultValue={this.state.inputValue }/>

当我们获取值的时候,就可以通过name进行获取到

在最新的版本中(修改为2017年12月份),React官方已经开始不建议这样使用,而是采用下面的方式使用,如下面代码:

<input ref={instance => this.instance = instance} type="text" defaultValue={this.state.inputValue }/>

在获取值得时候,能够通过 this.instance进行获取,如下面的使用实例

<html>
<head>
    <meta charset="utf-8">
    <title>学习React!!</title>
</head>
<body>
    <div id="app"></div>
    <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
    <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
    <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
    <script src="bower_components/react/react.js"></script>
    <script src="bower_components/react/react-dom.js"></script>
    <!--  注意下面注释掉的的方式是旧版本所使用的编译转换方式,没有注释的现在使用的-->
    <script src="bower_components/babel/browser.js"></script>
    <script type="text/babel">
      class FormApp extends React.Component {
          constructor(props) {
              super(props);
              this.handleSubmit = this.handleSubmit.bind(this);
              this.state = {
                  inputValue: 'input value',
                  selectValue: 'A',
                  textareaValue: 'some text here,,,,,'
              }
          }
          handleSubmit(e){
              e.preventDefault();
              var formData = {
                  input: ReactDOM.findDOMNode(this.instanceInput).value,
                  select:ReactDOM.findDOMNode(this.instanceSelect).value ,
                  textarea:ReactDOM.findDOMNode(this.instanceTextArea).value ,

              }
              console.log(this.instanceInput);
              console.log(this.instanceSelect);
              console.log(this.instanceTextArea);
              console.log(formData);
          }
          render(){
              return (
                      <form onSubmit={this.handleSubmit}>
                          <input ref={instanceInput => this.instanceInput = instanceInput} type="text" defaultValue={this.state.inputValue }/>
                          <br/>
                          选项:
                          <select defaultValue={ this.state.selectValue } ref={instanceSelect => this.instanceSelect = instanceSelect}>
                              <option value="A">A</option>
                              <option value="B">B</option>
                              <option value="C">C</option>
                              <option value="D">D</option>
                              <option value="E">E</option>
                          </select>
                          <br/>
                          <textarea defaultValue={this.state.textareaValue} ref={instanceTextArea => this.instanceTextArea = instanceTextArea}></textarea>
                          <br />
                          <button type="submit">提交</button>
                      </form>
              )
          }
      }
		var formApp = ReactDOM.render(
			<FormApp />,
			document.getElementById('app')
		)
	</script>
</body>
</html>

查看结果如下所示:


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

suwu150

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值