React createRef:引用
一 代码
import React, { Component } from 'react';
class Box extends Component {
render() {
return <button>你好</button>;
}
}
export default class MyComponent extends React.Component {
constructor(props) {
super(props);
this.inputRef = React.createRef(); // 将引用对象设置为父组件的成员
this.boxRef = React.createRef(); // 将引用对象设置为父组件的成员
}
render() {
return <div>
{/* ref属性设置在DOM组件上,current指向html元素 */}
<input type="text" ref={this.inputRef} />
{/* ref属性设置在普通组件上,current指向普通组件 */}
<Box ref={this.boxRef}/>
</div>
}
componentDidMount() {
console.log(this.inputRef); // {current: input}
console.log(this.inputRef.current); // <input type="text">
console.log(this.inputRef.current instanceof HTMLInputElement); // true
this.inputRef.current.focus(); // 操作子组件
console.log(this.boxRef); // {current: Box}
console.log(this.boxRef.current); // Box
console.log(this.boxRef.current instanceof React.Component); // true
}
}
二 原理
React.createRef函数会创建一个引用对象(只有一个current属性)。
// react安装包中的react.development.js
// an immutable object with a single mutable(易变的) value
function createRef() {
var refObject = {
current: null
};
{
Object.seal(refObject); // 将对象密封(不能增删属性、配置属性,但可以给属性赋值)
}
return refObject;
}
子组件创建完成后,会检测其html标签中的ref属性,并做相应的处理。
html标签中的key属性、ref属性,会被React特殊处理,不会出现在props属性中!
// react-dom安装包中的react-dom.development.js
function commitAttachRef(finishedWork) {
var ref = finishedWork.ref;
if (ref !== null) {
var instance = finishedWork.stateNode;
var instanceToUse = void 0;
switch (finishedWork.tag) {
case HostComponent: // 原生html标签
// 原样返回:function getPublicInstance(instance) {return instance;}
instanceToUse = getPublicInstance(instance);
break;
default:
instanceToUse = instance; // React组件
}
if (typeof ref === 'function') { // ref是函数
ref(instanceToUse); // 执行
} else { // ref是引用对象
{
if (!ref.hasOwnProperty('current')) {
warningWithoutStack$1(
false,
'Unexpected ref object provided for %s. '
+ 'Use either a ref-setter function or React.createRef().%s',
getComponentName(finishedWork.type), getStackByFiberInDevAndProd(finishedWork));
}
}
ref.current = instanceToUse; // 设置引用对象的current属性
}
}
}
引用对象是父组件的成员,于是父组件可以通过引用对象操作子组件。
React createRef:引用的更多相关文章
- React.createRef()
概述: 引用(Refs)提供了一个获得DOM节点或者创建在render方法中的React元素的方法: 在典型的React数据流中,props是唯一的父组件与它们的子元素的通信方式.更改子元素,你需要使 ...
- React的React.createRef()/forwardRef()源码解析(三)
1.refs三种使用用法 1.字符串 1.1 dom节点上使用 获取真实的dom节点 //使用步骤: 1. <input ref="stringRef" /> 2. t ...
- react之引用echarts
react之引用echarts npm: npm install echarts --save 代码: import React, { Component } from 'react'; // 引入 ...
- React中引用CSS样式的方法
相对于html中引用css的三种方法,react中也有三种方法,一一相对: 1. 行内样式:直接在组件内部定义 <div style={{width:'20px',height:'30px'}} ...
- react.js 引用 NavBar 报错svg-spite-loader
Navbar iconName="false" 配置 改为 iconName={this.props.bool}
- React + TypeScript:元素引用的传递
React 中需要操作元素时,可通过 findDOMNode() 或通过 createRef() 创建对元素的引用来实现.前者官方不推荐,所以这里讨论后者及其与 TypeScript 结合时如何工作. ...
- React forwardRef:跳转引用
一 在DOM组件中使用 import React, { Component } from 'react'; // 跳转引用对象本身并不关心ref,而是由渲染函数转发ref const FancyBut ...
- [React] Reference a node using createRef() in React 16.3
In this lesson, we look at where we came from with refs in React. Starting with the deprecated strin ...
- react中的DOM操作
前面的话 某些情况下需要在典型数据流外强制修改子代.要修改的子代可以是 React 组件实例,也可以是 DOM 元素.这时就要用到refs来操作DOM 使用场景 下面是几个适合使用 refs 的情况 ...
随机推荐
- java 中Math 的常用方法
public class Demo{ public static void main(String args[]){ /** *Math.sqrt()//计算平方根 *Math.cbrt()//计算立 ...
- Mysql组复制之单主模式(一)
环境 系统:CentOS release 6.9 (Final) Mysql:5.7 机器: S1 10.0.0.7 lemon S2 10.0.0.8 lemon2 S3 10.0.0.9 lemo ...
- webpack 中,module、chunk、bundle 的区别(待补充)
项目 区别 module 是开发中的单个模块 chunk 中文意思是"块",是指 webpack 在进行模块依赖分析的时候,代码分割出来的代码块 bundle
- mongodb集群配置副本集
测试环境 操作系统:CentOS 7.2 最小化安装 主服务器IP地址:192.168.197.21 mongo01 从服务器IP地址:192.168.197.22 mongo02 从服务器IP地址: ...
- win7中安装mysql
这篇文章主要介绍了如何在win7中安装mysql,所以加上了MySQL的下载过程,希望对需要的人有所帮助大家都知道MySQL是一款中.小型关系型数据库管理系统,很具有实用性,对于我们学习很多技术都有帮 ...
- Flask--(一对多)模型渲染表单数据
模型建立一一对多模型: 多表添加外键,建立两张表之间的关系 一表关联多表的属性,可以方便快速访问多表的数据 模板一层循环渲染一表数据,二层循环渲染多表的数据 代码展示: from flask impo ...
- PHP 获取数组是几维数组
// 判断数组是几维数组$data = array(); // 是你要判断的数组$al = array(0);function aL($data,&$al,$level=0){ if(is_a ...
- iframe-父子-兄弟页面相互传值(jq和js两种方法)
参考文章: http://blog.csdn.net/u013299635/article/details/78773207 http://www.cnblogs.com/xyicheng/archi ...
- 冷知识点:COLLATE 关键字是什么意思?
mysql 数据库表: CREATE TABLE `book_order_test` ( `order_id` varchar(50) COLLATE utf8_bin DEFAULT NULL CO ...
- docker network基础
前面介绍了nginx与php两个容器间是如何进行通信的: [root@docker ~]# docker run -d --name=php -v /www:/usr/local/nginx/html ...