首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >React-组件-Ref

React-组件-Ref

原创
作者头像
杨不易呀
发布2023-09-30 14:24:29
发布2023-09-30 14:24:29
5040
举报
文章被收录于专栏:杨不易呀杨不易呀

React 中获取元素的方式

  • 字符串
  • 对象
  • 回调函数

官方文档:https://zh-hans.reactjs.org/docs/refs-and-the-dom.html#gatsby-focus-wrapper

第一种

  • 传统方式(在 React 中及其不推荐)
代码语言:javascript
复制
import React from "react";

class App extends React.PureComponent {
    constructor(props) {
        super(props);
    }

    render() {
        console.log('App-render-被调用');
        return (
            <div>
                <p id={'box'}>我是段落</p>
                <button onClick={() => {
                    this.btnClick()
                }}>APP按钮
                </button>
            </div>
        )
    }

    btnClick() {
        let oP = document.getElementById('box');
        oP.innerHTML = 'www.it6666.top';
        console.log(oP);
    }
}

export default App;

第二种

  • 通过 ref='字符串' / this.refs.字符串 (通过字符串的方式即将被废弃, 也不推荐)
代码语言:javascript
复制
import React from "react";

class App extends React.PureComponent {
    constructor(props) {
        super(props);
    }

    render() {
        console.log('App-render-被调用');
        return (
            <div>
                <p ref={'box'}>我是段落</p>
                <button onClick={() => {
                    this.btnClick()
                }}>APP按钮
                </button>
            </div>
        )
    }

    btnClick() {
        let oP = this.refs.box;
        oP.innerHTML = 'www.it6666.top';
        console.log(oP);
    }
}

export default App;

第三种

  • 通过 createRef() 创建一个对象, 然后将这个对象传递给 ref (推荐)
代码语言:javascript
复制
import React from "react";

class App extends React.PureComponent {
    constructor(props) {
        super(props);
        this.oPRef = React.createRef();
    }

    render() {
        console.log('App-render-被调用');
        return (
            <div>
                <p ref={this.oPRef}>我是段落</p>
                <button onClick={() => {
                    this.btnClick()
                }}>APP按钮
                </button>
            </div>
        )
    }

    btnClick() {
        let oP = this.oPRef.current;
        oP.innerHTML = 'www.it6666.top';
        console.log(oP);
    }
}

export default App;

第四种

  • 通过传递一个回调函数, 然后保存回调函数参数的方式(推荐)
代码语言:javascript
复制
import React from "react";

class App extends React.PureComponent {
    constructor(props) {
        super(props);
        this.oPRef = null;
    }

    render() {
        console.log('App-render-被调用');
        return (
            <div>
                <p ref={(args) => {
                    this.oPRef = args
                }}>我是段落</p>
                <button onClick={() => {
                    this.btnClick()
                }}>APP按钮
                </button>
            </div>
        )
    }

    btnClick() {
        this.oPRef.innerHTML = 'www.it6666.top';
        console.log(this.oPRef);
    }
}

export default App;

React 中 Ref 注意点

  • 获取原生元素, 拿到的是元素本身
代码语言:javascript
复制
import React from "react";

class App extends React.PureComponent {
    constructor(props) {
        super(props);
        this.myRef = React.createRef();
    }

    render() {
        return (
            <div>
                <p ref={this.myRef}>我是段落</p>
                <button onClick={() => {
                    this.btnClick()
                }}>APP按钮
                </button>
            </div>
        )
    }

    btnClick() {
        console.log(this.myRef.current);
    }
}

export default App;
  • 获取类组件元素, 拿到的是组件的实例对象
代码语言:javascript
复制
import React from "react";

class Home extends React.PureComponent {
    render() {
        return (
            <div>Home</div>
        )
    }
}

class App extends React.PureComponent {
    constructor(props) {
        super(props);
        this.myRef = React.createRef();
    }

    render() {
        return (
            <div>
                <Home ref={this.myRef}/>
                <button onClick={() => {
                    this.btnClick()
                }}>APP按钮
                </button>
            </div>
        )
    }

    btnClick() {
        console.log(this.myRef.current);
    }
}

export default App;
  • 获取函数组件元素, 拿不到任何内容
代码语言:javascript
复制
import React from "react";

function About() {
    return (
        <div>About</div>
    )
}

class App extends React.PureComponent {
    constructor(props) {
        super(props);
        this.myRef = React.createRef();
    }

    render() {
        return (
            <div>
                <About ref={this.myRef}/>
                <button onClick={() => {
                    this.btnClick()
                }}>APP按钮
                </button>
            </div>
        )
    }

    btnClick() {
        console.log(this.myRef.current);
    }
}

export default App;
image-20220504131435516
image-20220504131435516

官方文档:https://zh-hans.reactjs.org/docs/refs-and-the-dom.html#gatsby-focus-wrapperundefined(https://foruda.gitee.com/images/1694531976417249641/dc6f8fed_5151444.png)

最后

本期结束咱们下次再见👋~

🌊 关注我不迷路,如果本篇文章对你有所帮助,或者你有什么疑问,欢迎在评论区留言,我一般看到都会回复的。大家点赞支持一下哟~ 💗

我正在参与2023腾讯技术创作特训营第二期有奖征文,瓜分万元奖池和键盘手表

输入图片说明
输入图片说明

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • React 中获取元素的方式
  • 第一种
  • 第二种
  • 第三种
  • 第四种
  • React 中 Ref 注意点
  • 最后
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档