美文网首页
React基础篇之组件组件实例三大属性refs

React基础篇之组件组件实例三大属性refs

作者: 硅谷干货 | 来源:发表于2021-12-09 09:37 被阅读0次

字符串形式的ref

<script type="text/babel">
  //创建组件
  class Demo extends React.Component{
    //展示左侧输入框的数据
    showData = ()=>{
      const {input1} = this.refs
      alert(input1.value)
    }
    //展示右侧输入框的数据
    showData2 = ()=>{
      const {input2} = this.refs
      alert(input2.value)
    }
    render(){
      return(
        <div>
          <input ref="input1" type="text" placeholder="点击按钮提示数据"/>&nbsp;
          <button onClick={this.showData}>点我提示左侧的数据</button>&nbsp;
          <input ref="input2" onBlur={this.showData2} type="text" placeholder="失去焦点提示数据"/>
        </div>
      )
    }
  }
  //渲染组件到页面
  ReactDOM.render(<Demo a="1" b="2"/>,document.getElementById('test'))
</script>

回调函数形式的ref

<script type="text/babel">
  //创建组件
  class Demo extends React.Component{
    //展示左侧输入框的数据
    showData = ()=>{
      const {input1} = this
      alert(input1.value)
    }
    //展示右侧输入框的数据
    showData2 = ()=>{
      const {input2} = this
      alert(input2.value)
    }
    render(){
      return(
        <div>
          <input ref={c => this.input1 = c } type="text" placeholder="点击按钮提示数据"/>&nbsp;
          <button onClick={this.showData}>点我提示左侧的数据</button>&nbsp;
          <input onBlur={this.showData2} ref={c => this.input2 = c } type="text" placeholder="失去焦点提示数据"/>&nbsp;
        </div>
      )
    }
  }
  //渲染组件到页面
  ReactDOM.render(<Demo a="1" b="2"/>,document.getElementById('test'))
</script>

回调ref中回调执行次数的问题

<script type="text/babel">
  //创建组件
  class Demo extends React.Component{

    state = {isHot:false}

    showInfo = ()=>{
      const {input1} = this
      alert(input1.value)
    }

    changeWeather = ()=>{
      //获取原来的状态
      const {isHot} = this.state
      //更新状态
      this.setState({isHot:!isHot})
    }

    saveInput = (c)=>{
      this.input1 = c;
      console.log('@',c);
    }

    render(){
      const {isHot} = this.state
      return(
        <div>
          <h2>今天天气很{isHot ? '炎热':'凉爽'}</h2>
          {/*<input ref={(c)=>{this.input1 = c;console.log('@',c);}} type="text"/><br/><br/>*/}
          <input ref={this.saveInput} type="text"/><br/><br/>
          <button onClick={this.showInfo}>点我提示输入的数据</button>
          <button onClick={this.changeWeather}>点我切换天气</button>
        </div>
      )
    }
  }
  //渲染组件到页面
  ReactDOM.render(<Demo/>,document.getElementById('test'))
</script>

createRef的使用

<script type="text/babel">
  //创建组件
  class Demo extends React.Component{
    /* 
      React.createRef调用后可以返回一个容器,该容器可以存储被ref所标识的节点,该容器是“专人专用”的
      */
    myRef = React.createRef()
    myRef2 = React.createRef()
    //展示左侧输入框的数据
    showData = ()=>{
      alert(this.myRef.current.value);
    }
    //展示右侧输入框的数据
    showData2 = ()=>{
      alert(this.myRef2.current.value);
    }
    render(){
      return(
        <div>
          <input ref={this.myRef} type="text" placeholder="点击按钮提示数据"/>&nbsp;
          <button onClick={this.showData}>点我提示左侧的数据</button>&nbsp;
          <input onBlur={this.showData2} ref={this.myRef2} type="text" placeholder="失去焦点提示数据"/>&nbsp;
        </div>
      )
    }
  }
  //渲染组件到页面
  ReactDOM.render(<Demo a="1" b="2"/>,document.getElementById('test'))
</script>
上一篇:React基础篇之组件实例三大属性props - 简书 (jianshu.com)
下一篇:React基础篇之事件处理 - 简书 (jianshu.com)

点赞加关注,永远不迷路

相关文章

  • React基础篇之组件组件实例三大属性refs

    字符串形式的ref 回调函数形式的ref 回调ref中回调执行次数的问题 createRef的使用 上一篇:Rea...

  • 2020-07-04 ref和$refs

    ref 给元素或子组件添加ref属性,则该元素或者子组件实例对象会被添加到当前组件实例对象下的$refs属性中 $...

  • React中ref详解+例子

    组件实例对象的3大属性之二: refs属性组件内的标签都可以定义ref属性来标识自己在组件中可以通过this.re...

  • vue进阶

    1. parent,$children 基础使用:参考vue官方文档 $refs 父组件调用子组件的方法或属性 $...

  • react-native 初体验

    react-native 常用组件 基础组件: 交互组件(跨平台) TabBarIos 常用属性: TabBarI...

  • 3.React中的refs属性的作用是什么?

    Refs是React提供给我们安全的访问DOM元素或者某个组件实例的句柄,我们可以为元素添加ref属性然后在回调函...

  • React获取DOM

    React获取DOM的方法简述 createRef 当 ref 属性在组件时,获取组件实例;当ref属性在dom时...

  • vue | 组件通信方式盘点

    父组件 => 子组件 属性 props 特性 $attrs 引用 refs 子元素$children 子组件 =>...

  • 十道前端面试题第【07】篇

    1、字节跳动三面之React面试 什么是虚拟DOM? 类组件和函数组件之间有什么区别? React中的refs作用...

  • React Native基础之Image

    React Native基础之Image Image组件 在React Native中,Image组件是用来...

网友评论

      本文标题:React基础篇之组件组件实例三大属性refs

      本文链接:https://www.haomeiwen.com/subject/txmvxrtx.html