react组件实例三大属性之refs

ref官网说明

01-字符串的refs => 少用不推荐

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>组件实例三大属性--refs</title>
</head>
<body>
  <div id="test"></div>
  
  <script type="text/javascript" src="../react_js/react.development.js"></script>
  <script type="text/javascript" src="../react_js/react-dom.development.js"></script>
  <script type="text/javascript" src="../react_js/babel.min.js"></script>

  <script type="text/babel">
    class RefsString extends React.Component{
      showData=()=>{
        console.log(this.refs.input1.value);
      }
      showDataTwo=()=>{
        console.log(this.refs.input2.value);
      }
      render(){
        return (
          <div>
            <input ref='input1' type="text" placeholder='点击按钮获取值'/>
            <button onClick={this.showData}>按钮</button>
            <input ref='input2' onBlur={this.showDataTwo} type="text" placeholder='失去焦点获取值'/>
          </div>
        )
      }

    }

    ReactDOM.render(<RefsString/>,document.getElementById('test'))
  </script>
</body>
</html>

在这里插入图片描述

02-回调函数的refs => 内联写法和class的绑定函数写法

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>组件实例三大属性--refs</title>
</head>
<body>
  <div id="test"></div>
  
  <script type="text/javascript" src="../react_js/react.development.js"></script>
  <script type="text/javascript" src="../react_js/react-dom.development.js"></script>
  <script type="text/javascript" src="../react_js/babel.min.js"></script>

  <script type="text/babel">
    class RefsString extends React.Component{
      showData=()=>{
        console.log(this.input1.value);
      }
      showData2=()=>{
        console.log(this.input3.value);
      }
      showDataTwo=()=>{
        console.log(this.input2.value);
      }
      callBack1=(c)=>{
        this.input1 = c
      }
      callBack2=(c2)=>{
        this.input2=c2
      }
      render(){
        return (
          <div>
            <input ref={(c)=>{console.log(c,'内联写法');this.input3=c}} type="text" placeholder='点击按钮获取值=>内联'/>
            <input ref={this.callBack1} type="text" placeholder='点击按钮获取值'/>
            <button onClick={this.showData}>按钮</button>
            <button onClick={this.showData2}>内联按钮</button>
            <input onBlur={this.showDataTwo} ref={this.callBack2} onBlur={this.showDataTwo} type="text" placeholder='失去焦点获取值'/>
          </div>
        )
      }

    }

    ReactDOM.render(<RefsString/>,document.getElementById('test'))
  </script>
</body>
</html>

03-createRef的使用 => 推荐写法(最新)

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>组件实例三大属性--refs</title>
</head>
<body>
  <div id="test"></div>
  
  <script type="text/javascript" src="../react_js/react.development.js"></script>
  <script type="text/javascript" src="../react_js/react-dom.development.js"></script>
  <script type="text/javascript" src="../react_js/babel.min.js"></script>

  <script type="text/babel">
    class RefsString extends React.Component{
      // React.createRef()返回一个容器, 容器里面包裹ref标识的节点, 而且容器里面只能有一个节点(专人专用)
      MyRef1 = React.createRef();
      MyRef2 = React.createRef();

      showData=()=>{
        console.log(this.MyRef1.current.value);
      }
      showDataTwo=()=>{
        console.log(this.MyRef2.current.value);
      }
      render(){
        return (
          <div>
            <input ref={this.MyRef1} type="text" placeholder='点击按钮获取值'/>
            <button onClick={this.showData}>按钮</button>
            <input ref={this.MyRef2} onBlur={this.showDataTwo} type="text" placeholder='失去焦点获取值'/>
          </div>
        )
      }

    }

    ReactDOM.render(<RefsString/>,document.getElementById('test'))
  </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值