[React] Forward a DOM reference to another Component using forwardRef in React 16.3
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的更多相关文章
- react中的DOM操作
前面的话 某些情况下需要在典型数据流外强制修改子代.要修改的子代可以是 React 组件实例,也可以是 DOM 元素.这时就要用到refs来操作DOM 使用场景 下面是几个适合使用 refs 的情况 ...
- JavaScript是如何工作的:编写自己的Web开发框架 + React及其虚拟DOM原理
这是专门探索 JavaScript 及其所构建的组件的系列文章的第 19 篇. 如果你错过了前面的章节,可以在这里找到它们: JavaScript 是如何工作的:引擎,运行时和调用堆栈的概述! Jav ...
- react ref获取dom对象
react文档 step = React.createRef(); // init <div ref={this.step}></div> // bind componentD ...
- react的非DOM操作
非dom属性?dangerouslySetInnerHTML,ref,key非dom标准属性,也就是说dom标准里面没有规定的属性,react引入了三个非dom属性,如上. dangerouslySe ...
- react 的虚拟dom
前端优化的主要方面就是减少页面的DOM操作,减少重排和重绘,React在这方面做了优化,采用了所谓的虚拟DOM,其实我们平时也会遇到虚拟DOM,只是你没有注意罢了,请听我娓娓道来. 所谓的虚拟DOM ...
- React:关于虚拟DOM(Virtual DOM)
Virtual DOM 是一个模拟 DOM 树的 JavaScript 对象. React 使用 Virtual DOM 来渲染 UI,当组件状态 state 有更改的时候,React 会自动调用组件 ...
- React virtual DOM explained in simple English/简单语言解释React的虚拟DOM
初学React,其中一个很重要的概念是虚拟DOM,看了一篇文章,顺带翻译一下. If you are using React or learning React, you must have hear ...
- 深入理解 React 的 Virtual DOM
React在前端界一直很流行,而且学起来也不是很难,只需要学会JSX.理解State和Props,然后就可以愉快的玩耍了,但想要成为React的专家你还需要对React有一些更深入的理解,希望本文对你 ...
- React的虚拟DOM
ReactJs的一大特点就是引进了虚拟dom(Virtual DOM)的概念.为什么我们需要Virtual DOM,Virtual DOM给我们带来了什么优势. 首先我们要了解一下浏览器的工作流. 当 ...
随机推荐
- 如何在ashx处理页中获取Session值
本文章摘自:http://www.cnblogs.com/vihone/archive/2010/06/04/1751490.html 在一般事务处理页面,可以轻松的得到 Request,Respon ...
- 10 Future Web Trends 十大未来互联网趋势
转载自:http://blog.sina.com.cn/s/blog_4be577310100ajpb.html 我们很满意自己进入的当前网络纪元,通常被称为Web 2.0.这个阶段互联网的特征包括搜 ...
- E20170905-mk
recursive adj. 回归的,递归的;
- Django day07 (二)单表操作
单表操作 -mysql数据库:settings里配置: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME ...
- Win10重置 系统诸多设置或者菜单点击无效或者异常信息回复办法
cmd: 输入下列脚本重新注册DLL文件,待执行完毕后重启电脑 for %1 in (%windir%\system32\*.dll) do regsvr32.exe /s %1
- UILabel垂直方向显示(上下的顺序显示)。
NSString* text = @"一"; NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFont ...
- xml之基本操作
XML:Extensible Markup Language(可扩展标记语言)的缩写,是用来定义其它语言的一种元语言,其前身是SGML(Standard Generalized Markup Lang ...
- lnmp 安装FTP服务 并配置FTP用户
lnmp 默认是不带FTP服务的,需要的童鞋要自行安装.步骤也很简单 一,进入lnmp目录,找到pureftpd.sh 二,直接运行该脚本 ./pureftpd.sh 按任意键开始安装,等待,安装成功 ...
- 安卓通过UDP协议传输数据,中文乱码的问题
公司最近需要往智能家居方面发展,需要用到UDP协议传输数据,在网上找到了一些资料,但是发现传输中文的时候有乱码的现象,经过我多番捣鼓,终于解决了这个问题,下面贴上关键代码 客户端: public cl ...
- WEB笔记-3、盒子模型+定位+显示
3.1 盒子模型 边距控制 margin/padding:上 右 下 左: padding:内容和边距之间的空间 margin:”盒子“外撑开的空间,两个相邻标签外边距会出现重叠和累加的现象, ...