React@16.x(11)ref

1,介绍

reference 引用。和 vue 中的 refs 类似,同样不属于子组件的 props

场景

  • 需要直接操作 DOM 元素。比如让输入框聚焦。
  • 需要使用子组件中的方法。

1.1,得到的结果

  1. 作用于内置 HTML 元素,得到真实DOM对象。
  2. 作用于类组件,得到类的实例对象。
  3. 不能作用于函数组件

举例:

import React, { Component } from "react";
import MyComp from "./components/MyComp";

export default class App extends Component {
    handleClick = () => {
        this.refs.comp.method1();
        console.log(this.refs.h2.innerText);
    };
    render() {
        return (
            <>
                <MyComp ref="comp"></MyComp>
                <h2 ref="h2">h2元素</h2>
                <button onClick={this.handleClick}>获取 ref</button>
            </>
        );
    }
}

MyComp 组件:

import React, { Component } from "react";

export default class MyComp extends Component {
    method1 = () => {
        console.log("子组件的方法");
    };

    render() {
        return <div>子组件</div>;
    }
}

2,参数类型

render 执行时赋值,所以在 render 中获取为 undefined,在 componentDidMount 中可直接使用。

2.1,字符串(不再推荐)

上面已演示过,不再赘述。

2.2,对象

import React, { Component } from "react";
import MyComp from "./components/MyComp";

export default class App extends Component {
    constructor(props) {
        super(props);
        this.comp = React.createRef();
        this.h2 = React.createRef();
    }
    handleClick = () => {
        this.comp.current.method1();
        console.log(this.h2.current.innerText);
    };
    render() {
        return (
            <>
                <MyComp ref={this.comp}></MyComp>
                <h2 ref={this.h2}>h2元素</h2>
                <button onClick={this.handleClick}>获取 ref</button>
            </>
        );
    }
}

通过 React.createRef() 创建。该方法返回值就是一个对象,相当于:

constructor(props) {
    super(props);
    this.comp = {
        current: null,
    };
    this.h2 = {
        current: null,
    };
}

2.3,函数

import React, { Component } from "react";
import MyComp from "./components/MyComp";

export default class App extends Component {
    getRefComp = (el) => {
        this.comp = el;
    };
    getRefH2 = (el) => {
        this.h2 = el;
    };

    handleClick = () => {
        this.comp.method1();
        console.log(this.h2.innerText);
    };
    render() {
        return (
            <>
                <MyComp ref={this.getRefComp}></MyComp>
                <h2 ref={this.getRefH2}>h2元素</h2>
                <button onClick={this.handleClick}>获取 ref</button>
            </>
        );
    }
}

如果函数写在 render 中,那当 render 执行时,该函数会执行2次:

import React, { Component } from "react";

export default class App extends Component {
    state = {
        show: true,
    };
    handleClick = () => {
        this.setState({
            show: !this.state.show // 想再次执行 render
        });
    };
    render() {
        return (
            <>
                <h2
                    ref={(el) => {
                        console.log("el", el);
                        this.h2= el;
                    }}
                ></h2>
                <button onClick={this.handleClick}>获取 ref</button>
            </>
        );
    }
}

执行结果:

在这里插入图片描述

函数调用时机

1,render 执行时会调用该函数。

以上面的例子来说,按照执行顺序打印 h2 时,
render 打印 undefined(该函数第一次还没执行),该函数打印 h2 元素,componentDidMount 打印 h2元素。

2,如果 ref 的值发生变动(旧函数被新函数替代),则分别调用旧新函数,时间点在 componentDidUpdate 之前。

  • 旧函数调用时,传递 null
  • 新函数调用时,传递对象。

3,如果 ref 所在组件被卸载,也会调用函数,传递 null

3,注意点

注意使用场景,能使用属性和状态控制,就不要用 ref


以上。

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

下雪天的夏风

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

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

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

打赏作者

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

抵扣说明:

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

余额充值