• React中ref的使用方法和使用场景(详解)


    摘要

    不管在Vue中还是React,如果我们想使用一个元素的DOM,不需要通过JS中操纵DOM的方法,它们提供了一个专属的API就是ref。

    而Vue中的ref可能比较简单,这一篇主要讲一下如何在React中使用ref,以及使用ref的场景。

    1.ref的挂载

    在React中,ref可以挂载到html元素上,同时也可以挂载在React元素上,看下面的代码:

    import React, { Component } from 'react'
    // import { findDOMNode } from 'react-dom'
    import Child from './Child'
    
    export default class Father extends Component {
    
      componentDidMount(){
        console.log(this.refs.refElement);
        console.log(this.refs.child);
      }
    
      render() {
        return (
          <div>
            <input ref={ 'refElement' }></input>
            <Child ref={ 'child' }/>
            <button onClick={this.fn}>123</button>
          </div>
        )
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    控制台的打印为:

    在这里插入图片描述
    可以看到,在React中,ref是可以挂载到HTML元素和React元素上的。

    (1)挂载HTML元素,返回真实的DOM
    (2)挂载React元素,返回render后的实例对象

    同时React也提供了一个方法findDOMNode可以将React元素的ref返回变成真实的DOM元素。

    	import { findDOMNode } from 'react-dom'
        console.log(findDOMNode(this.refs.child));
    
    • 1
    • 2

    同时在上面的代码我们也可以看出来,ref的挂载是在componentDidMount等生命周期之前执行的

    2.使用ref的三种方式

    (1)字符串的方式

    import React, { Component } from 'react'
    
    export default class Father extends Component {
    
      componentDidMount(){
        console.log(this.refs.refElement);
      }
    
      render() {
        return (
          <div>
            <input ref={ 'refElement' }></input>
            <button onClick={this.fn}>123</button>
          </div>
        )
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    这种方式和Vue的ref比较相似,但是官方目前已经不推荐使用该方式,后续可能还会废弃。

    (2)函数的方式

    import React, { Component } from 'react'
    
    export default class Father extends Component {
    
      componentDidMount(){
        console.log(this.refElement);
      }
    
      render() {
        return (
          <div>
            <input ref={ ref => this.refElement = ref }></input>
            <button onClick={this.fn}>123</button>
          </div>
        )
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    (3)react.CreateRef的方式

    import React, { Component } from 'react'
    
    export default class Father extends Component {
    
      refElement = React.createRef();
    
      componentDidMount(){
        console.log(this.refElement.current);
      }
    
      render() {
        return (
          <div>
            <input ref={this.refElement}></input>
            <button onClick={this.fn}>123</button>
          </div>
        )
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    记住这里面通过refElement中的current,获取真实的DOM元素。

    3.ref的使用场景

    这里我们说一个比较常见的场景,就是点击按钮让输入框聚焦:

    import React, { Component } from 'react'
    
    export default class Father extends Component {
    
      refElement = React.createRef();
    
      componentDidMount(){
        console.log(this.refElement.current);
      }
    
      fn = ()=>{
        this.refElement.current.focus();
      }
    
      render() {
        return (
          <div>
            <input ref={this.refElement}></input>
            <button onClick={this.fn}>聚焦</button>
          </div>
        )
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    通过获取DOM后,调用DOM上的focus方法API,来让input框进行聚焦。

    同时ref也可以适用于一些DOM元素的动画效果,例如移动,变大变小,都需要通过ref来控制DOM,进行操作。

  • 相关阅读:
    2.mysql的安装
    快速找到需要包含函数的头文件
    Vue 中修改了计算属性(computed) 或者它的依赖变量,计算属性的值没有变化的几种情况
    Spring Boot复用yaml文件
    xen-timer
    国内券商有没有量化交易接口,哪家做的比较好
    【数据结构与算法】四、链表相关算法题
    聊聊大模型的微调实现及其应用
    如何安装和使用three.js
    147. SAP UI5 SmartTable 控件的使用介绍
  • 原文地址:https://blog.csdn.net/weixin_46726346/article/details/127545244