React forwardRef:跳转引用

本文详细介绍了React中Ref的三种使用场景:在DOM组件、高阶组件中使用及其实现原理。通过实例展示了如何在自定义组件中传递Ref,实现对组件内部元素的直接操作。

 

一 在DOM组件中使用

 

import React, { Component } from 'react';

// 跳转引用对象本身并不关心ref,而是由渲染函数转发ref
const FancyButton = React.forwardRef((props, ref) => (
    <button ref={ref} className="FancyButton">
        {props.children}
    </button>
));

class App extends Component {
    constructor(props) {
        super(props);
        this.myRef = React.createRef();
    }
    click() {
        this.myRef.current.focus();
        console.log(this.myRef.current.innerHTML)
    }
    render() {
        return (
            <div>
                <FancyButton ref={this.myRef}>光彩夺目的按钮</FancyButton>
                <div onClick={this.click.bind(this)}>点击我</div>
            </div>
        );
    }
}

export default App;

 

 

二 在高阶组件中使用

 

1 app.js

 

import React, { Component } from 'react';
import FancyButton from './fancyButton.jsx';

class App extends Component {
  constructor(props) {
    super(props);
    this.btnRef = React.createRef(); // 创建引用对象
  }
  click(){
    this.btnRef.current.print();
  }
  render() {
    return (
      <div>
        <FancyButton ref={this.btnRef} />
        <div onClick={this.click.bind(this)}>点击</div>
      </div>

    );
  }
}

export default App;

 

 

2 logProps.js

 

import React from 'react';

export default function logProps(Component) {
    // 高阶组件
    class LogProps extends React.Component {
        componentDidUpdate(prevProps) {
            console.log('old props:', prevProps);
            console.log('new props:', this.props);
        }

        render() {
            const { myForwardRef, ...rest } = this.props;
            // 引用对象的current属性指向被包裹组件
            return <Component ref={myForwardRef} {...rest} />;
        }
    }

    // 将高阶组件包裹在跳转引用对象中。
    return React.forwardRef((props, ref) => {
        // 将ref属性转换成高阶组件的自定义属性,将引用对象转移到了高阶组件的props属性中。
        // 防止React自动处理组件的ref属性。
        return <LogProps {...props} myForwardRef={ref} />;
    });
}

 

3 fancyButton.js

 

import React, { Component } from 'react';
import logProps from './logProps.jsx';

class FancyButton extends Component {
    print(){
        console.log('色彩夺目的按钮');
    }
    render() {
        return <button>色彩夺目的按钮</button>
    }
}

export default logProps(FancyButton);

 

 

三 原理

 

1 React.forwardRef函数,只是创建一个跳转引用对象。

 

 

2 跳转引用对象(对象)也可以用作JSX语法的标签名,作用与组件(类)类似。但主要作用是提供渲染函数,转发props和ref。

 

3 React会在合适的时机,自动调用跳转引用对象的render方法,获取ReactElement。

 

转载于:https://www.cnblogs.com/sea-breeze/p/10461271.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值