The function forwardRef allows us to extract a ref and pass it to its descendants. This is a powerful tool, but should be used with caution to avoid unexpected ref behaviour. The technique of forwarding refs really starts to shine in combination with higher order components (HOCs).

forwardRef should be used when you are using HOC and pasing the ref around from parent to child component.

parent component:

import React, { Component } from "react";
import TextInput from "./TextInput"; class App extends Component {
inputRef = React.createRef(); focusInput = () => {
this.inputRef.current.focus();
}; render() {
return (
<React.Fragment>
<TextInput inputRef={this.inputRef} />
<button onClick={this.focusInput}>Focus</button>
</React.Fragment>
);
}
} export default App;

child commponent:

import React, { Component, Fragment } from "react";
import logProps from "./logProps"; class TextInput extends Component {
render() {
return <input ref={this.props.inputRef} />;
}
} export default logProps(TextInput);

HOC componnet:

import React, { Component } from "react";

function logProps(Component) {
class LogProps extends Component {
componentDidUpdate(prevProps) {
console.log("before update", prevProps);
console.log("after update", this.props);
} render() {
const { forwardRef, ...rest } = this.props;
return <Component {...rest} ref={forwardRef} />;
}
} const forwardRef = (props, ref) => {
return <LogProps forwardRef={ref} {...props} />;
}; const name = Component.displayName || Component.name;
forwardRef.displayName = `logProps(${name})`; return React.forwardRef(forwardRef);
} export default logProps;

Display name:

Becasue HOC component doesn't have a correct display name in the dev tool, we need to set it up:

  const forwardRef = (props, ref) => {
return <LogProps forwardRef={ref} {...props} />;
}; const name = Component.displayName || Component.name;
forwardRef.displayName = `logProps(${name})`; return React.forwardRef(forwardRef);

[React] Forward a DOM reference to another Component using forwardRef in React 16.3的更多相关文章

  1. react中的DOM操作

    前面的话 某些情况下需要在典型数据流外强制修改子代.要修改的子代可以是 React 组件实例,也可以是 DOM 元素.这时就要用到refs来操作DOM 使用场景 下面是几个适合使用 refs 的情况 ...

  2. JavaScript是如何工作的:编写自己的Web开发框架 + React及其虚拟DOM原理

    这是专门探索 JavaScript 及其所构建的组件的系列文章的第 19 篇. 如果你错过了前面的章节,可以在这里找到它们: JavaScript 是如何工作的:引擎,运行时和调用堆栈的概述! Jav ...

  3. react ref获取dom对象

    react文档 step = React.createRef(); // init <div ref={this.step}></div> // bind componentD ...

  4. react的非DOM操作

    非dom属性?dangerouslySetInnerHTML,ref,key非dom标准属性,也就是说dom标准里面没有规定的属性,react引入了三个非dom属性,如上. dangerouslySe ...

  5. react 的虚拟dom

    前端优化的主要方面就是减少页面的DOM操作,减少重排和重绘,React在这方面做了优化,采用了所谓的虚拟DOM,其实我们平时也会遇到虚拟DOM,只是你没有注意罢了,请听我娓娓道来.  所谓的虚拟DOM ...

  6. React:关于虚拟DOM(Virtual DOM)

    Virtual DOM 是一个模拟 DOM 树的 JavaScript 对象. React 使用 Virtual DOM 来渲染 UI,当组件状态 state 有更改的时候,React 会自动调用组件 ...

  7. React virtual DOM explained in simple English/简单语言解释React的虚拟DOM

    初学React,其中一个很重要的概念是虚拟DOM,看了一篇文章,顺带翻译一下. If you are using React or learning React, you must have hear ...

  8. 深入理解 React 的 Virtual DOM

    React在前端界一直很流行,而且学起来也不是很难,只需要学会JSX.理解State和Props,然后就可以愉快的玩耍了,但想要成为React的专家你还需要对React有一些更深入的理解,希望本文对你 ...

  9. React的虚拟DOM

    ReactJs的一大特点就是引进了虚拟dom(Virtual DOM)的概念.为什么我们需要Virtual DOM,Virtual DOM给我们带来了什么优势. 首先我们要了解一下浏览器的工作流. 当 ...

随机推荐

  1. K-means algorithm----PRML读书笔记

    The K-means algorithm is based on the use of squared Euclidean distance as the measure of  dissimila ...

  2. web.xml中load-on-startup

    <servlet> <servlet-name>SystemInit</servlet-name> <servlet-class>sjgl.system ...

  3. 8.20noip模拟题

    2017-8-20 NOIP模拟赛 by coolyangzc 共3道题目,时间3.5小时 题目名 机器人 数列 虫洞 源文件 robot.cpp/c/pas seq.cpp/c/pas holes. ...

  4. Win10重置 系统诸多设置或者菜单点击无效或者异常信息回复办法

    cmd: 输入下列脚本重新注册DLL文件,待执行完毕后重启电脑 for %1 in (%windir%\system32\*.dll) do regsvr32.exe /s %1

  5. 【DP】编辑距离

    日常吐槽:关于DP,有一种莫名的恐惧...maybe源于与mtw大佬与quantum11大佬,初中时抬老师爬楼梯的经历... 言归正传: 编辑距离 [题目描述] 设A和B是两个字符串.我们要用最少的字 ...

  6. mybatis parameterType报错:There is no getter for property named 'xxx' in 'class java.lang.String'

    方法1: 当parameterType = "java.lang.String" 的时候,参数读取的时候必须为 _parameter 方法2: 在dao层的时候,设置一下参数,此方 ...

  7. 谈谈对Java中Unicode、编码的理解

    我们经常会遇到编码问题.Java号称国际化的语言,是因为它的class文件采用UTF-8,而JVM运行时使用UTF-16(至于为什么JVM中要采用UTF-16,我没看过 相关的资料,但我猜可能是因为J ...

  8. C# html生成PDF遇到的问题,从iTextSharp到wkhtmltopdf

    我们的网站业务会生成一个报告,用网页展示出来,要有生成pdf并下载的功能,关键是生成pdf. 用内容一段段去拼pdf,想想就很崩溃,所以就去网上找直接把html生成pdf的方法. 网上资料大部分都是用 ...

  9. WPF TextBox 仅允许输入数字

    因为在 IValueConverter 实现中,当文本不能转换为目标类型时返回 DependencyProperty.UnsetValue ,Validation.GetHasError 返回 tru ...

  10. CDN 内容分发网络

    第一步,HTML的文件引用:HTML的文件头(也有文件中,文件尾)那边常有其他文件引用,比如CSS以及JS的引用. 就以bootstrap常用的引用来举个栗子你常见的引用可能会是这样的: <he ...