一 在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。

React forwardRef:跳转引用的更多相关文章

  1. React + TypeScript:元素引用的传递

    React 中需要操作元素时,可通过 findDOMNode() 或通过 createRef() 创建对元素的引用来实现.前者官方不推荐,所以这里讨论后者及其与 TypeScript 结合时如何工作. ...

  2. React路由 + 绝对路径引用

    路由: 哈希路由(在url地址后加   #name) // 实现页面监听 window.onhashchange = function(){ console.log(‘hash:’,window.lo ...

  3. React Ref 和 React forwardRef

    Ref 和Dom,Ref是reference(引用)的简写. 能力:大多数情况下,props前递可以解决一切问题,但是依然有需要触达React实例或者Dom节点的情况,这时候应该使用React Ref ...

  4. react之本地图片引用

    react之本地图片引用 <img src="../images/photo.png"/> 这种写法在react中是不支持的,所以引用本地图片需要用import或者re ...

  5. refs转发 React.forwardRef

    2020-04-01 refs转发 前几天刚总结完ref&DOM之间的关系,并且想通了3种ref的绑定方式 今天总结一下refs转发 这是react中一直困扰我的一个点 示例: 输入: wor ...

  6. React Hooks & react forwardRef hooks & useReducer

    React Hooks & react forwardref hooks & useReducer react how to call child component method i ...

  7. react界面跳转,滚动到顶部

    在使用react-router-dom时,我们经常会遇到路由切换时滚动到浏览器顶部的问题. 滚动到顶部 Scroll to top 很多时候我们需要的是滚动到顶部“Scroll to top”,因为发 ...

  8. react 或 vue 中引用 jQuery 插件

    前言 今天与遇到一个令人抓狂的事情, 因为项目中有个交互太过于复杂而且冷门, 没有人封装类似react-swiper那种的移植过来的插件 只有现成的jQuery插件. 而时间并不宽裕,自己重写成rea ...

  9. vue 和 react 路由跳转和传参

                      react  1 .跳转方式加传参 this.props.history.push({ //地址 pathname: '/film/Details', //路由传参 ...

随机推荐

  1. go语言学习--go中godep的使用小结

    go中的godep 本文参考:http://www.cnblogs.com/me115/p/5528463.html#h20 http://studygolang.com/articles/4385 ...

  2. 基于STM8的ADC读取---STM8-第四章

    1. 综诉 想学会如何在STM8上使用ADC这个功能,我们先得了解单片机中ADC究竟是什么. ADC是模拟信号转成数值信号,单片机只能识别TTL电平,其实就是 1 或者 0 ,但是如果我们给它一个3. ...

  3. GSM与GPRS的区别

    1.    GSM是全球移动通讯系统(Global System for Mobile Communications)的简称 2.    GPRS是通用分组无线业务(General Packet Ra ...

  4. Python中的取模运算

    C++中的取模运算符%只能对整数使用(如果要对浮点数使用需要fmod),Python则不同,对整数或浮点数均有效. 在这里再介绍一下取模的定义:假设a,b两个数,那么a mod b = a - n*b ...

  5. {"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}

    ElasticSearch-head 查询报 406错误码 {"error":"Content-Type header [application/x-www-form-u ...

  6. Spring 回滚事务@Transactional

    @Transactional   spring 事务注解 默认遇到throw new RuntimeException("...");会回滚 需要捕获的throw new Exce ...

  7. Linux守护进程管理利器——Supervisor

    Supervisor是采用 Python(2.4+) 开发的,它是一个允许用户管理 基于 Unix 系统进程的 Client/Server 系统,提供了大量功能来实现对进程的管理.安装: yum in ...

  8. H5-meta标签使用大全

    meta标签有下面的作用:搜索引擎优化(提高搜索性能),控制页面功能化. meta标签的组成:meta标签共有两个属性,它们分别是http-equiv属性和name属性. 1.name属性 name属 ...

  9. EF core2.1+MySQL报错'Void Microsoft.EntityFrameworkCore.Storage.Internal.RelationalParameterBuilder..ctor(Microsoft.EntityFrameworkCore.Storage.IRelationalTypeMapper)

    一.使用.net core 2.0 EF mysql 运行一直报错如下: An unhandled exception occurred while processing the request. M ...

  10. Springboot admin 发送邮件失败:com.sun.mail.smtp.SMTPSenderFailedException: 553 Mail from must equal authorized user

    发邮件已经是老生常谈了,今天又遇到了,而且又出了各种问题.我晕哦. 我的配置是: spring.mail.host=smtp..com spring.mail.username=klxxxx spri ...