React从入门到精通系列之(14)refs和DOM元素
3.7k 次阅读 · 读完需要 8 分钟
十四、refs和DOM元素
在典型的React数据流中,props是父组件与其子组件交互的唯一方式。 要修改子组件,需要使用一个新的props进行重新渲染。
但是,在某些情况下,您需要在典型数据流之外强制修改子组件。 要修改的子组件可以是React组件实例,也可以是DOM元素。 对于这两种情况,React提供了一个以下这样的功能。
通过ref属性设置回调函数
React提供可以附加到任何组件的特殊属性。 ref属性接受一个回调函数,回调函数将在组件被挂载或卸载后立即执行。
当在HTML元素上使用ref属性时,ref回调函数接收一个基础的DOM元素作为其参数。 例如,此代码使用ref回调函数来存储对DOM节点的引用:
import React from 'react';
import ReactDOM from 'react-dom';
class CustomTextInput extends React.Component {
constructor(props) {
super(props);
this.focus = this.focus.bind(this);
}
focus() {
// textInput是一个标准的DOM元素
this.textInput.focus();
}
render() {
return (
<div>
<input type="text" ref={input => {
this.textInput = input;
}}/>
<input type="button" value="选中上面的text input" onClick={this.focus}/>
</div>
);
}
}
ReactDOM.render(
<CustomTextInput/>,
document.getElementById('root')
);
当组件装载(mounting)时,React将使用DOM元素调用ref回调函数,并在卸载时用null调用它。
使用ref回调函数是为类设置一个属性来访问DOM元素的常见模式。 如果您目前正在使用this.refs.myRefName来访问DOM引用的话,我会建议你使用此模式。
当在自定义组件上使用ref属性时,ref回调接收组件的已装入的组件实例作为其参数。 例如,如果我们想要包装上面的CustomTextInput来模拟它在装载(mounting)后立即被点击:
class AutoFocusTextInput extends React.Component {
componentDidMount() {
this.textInput.focus();
}
render() {
return (
<CustomTextInput ref={input => {this.textInput = input; }} />
);
}
}
您不能在功能性组件上使用ref属性,因为它们没有实例。 但是,您可以使用功能性组件的render函数内的ref属性:
function CustomTextInput(props) {
// 这里必须提前顶一个textInput,只有这样才可以正常执行ref回调函数
let textInput = null;
function click() {
textInput.focus();
}
return (
<div>
<input type="text" ref={input => { textInput = input; }} />
<input type="button" value="选中这个输入框" onClick={click} />
</div>
);
}
不要过度使用ref
你的第一个倾向可能是使用refs在你的应用中“make things happen”。
如果是这种情况,你必须花一点时间,关键去考虑在组件层次结构中应该拥有什么状态。
通常,在层次结构中处于更高级别的组件“拥有”状态是一个让一切便清除的最适当位置。 有关示例,请参阅本系列的第10篇《提升state》。
React从入门到精通系列之(14)refs和DOM元素的更多相关文章
- 办公软件Office PPT 2010视频教程从入门到精通系列教程(22课时)
办公软件Office PPT 2010视频教程从入门到精通系列教程(22课时) 乔布斯的成功离不开美轮美奂的幻灯片效果,一个成功的商务人士.部门经理也少不了各种各样的PPT幻灯片.绿色资源网给你提供了 ...
- Provisioning Services 7.6 入门到精通系列之一:PVS前期规划
1. Provisioning Services 产品概述 Provisioning Services (简称PVS)采用了一种与传统映像解决方案截然不同的方法,从根本上改变了硬件与依托硬件而运行的 ...
- Jenkins pipeline 入门到精通系列文章
Jenkins2 入门到精通系列文章. Jenkins2 下载与启动jenkins2 插件安装jenkins2 hellopipelinejenkins2 pipeline介绍jenkins2 jav ...
- NHibernate从入门到精通系列
http://www.cnblogs.com/GoodHelper/archive/2011/02/17/1948744.html NHibernate从入门到精通系列(4)——持久对象的生命周期(上 ...
- Selenium 入门到精通系列:六
Selenium 入门到精通系列 PS:Checkbox方法 例子 HTML: <html> <head> <title>测试页面</title> &l ...
- Selenium 入门到精通系列:五
Selenium 入门到精通系列 PS:显式等待.隐式等待.强制等待方法 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019 ...
- Selenium 入门到精通系列:四
Selenium 入门到精通系列 PS:鼠标右键.鼠标悬停.键盘操作方法 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019 ...
- Selenium 入门到精通系列:三
Selenium 入门到精通系列 PS:Driver_Element 常用方法 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2 ...
- Selenium 入门到精通系列:二
Selenium 入门到精通系列 PS:用户登录 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019-04-23 16:12 ...
随机推荐
- [OpenCV]实验1.1:图像加载、显示
实验要求:利用图像库的功能,实现从文件加载图像,并在窗口中进行显示的功能:利用常见的图像文件格式(.jpg;.png;.bmp; .gif)进行测试 实验原理:图片读取到程序中是以Mat结构存储的,在 ...
- 使用LNMP常见问题解答
使用LNMP常见问题解答 一.LNMP的安装过程详解,注:绿色文字为注释,实际使用过程中没有. ##先要下载,并根据不同系统进行安装: wget -c http://soft.vpser.net/ln ...
- 利用SmtpClient发送邮件
1 163邮箱 HOST:smtp.163.com public static string CreateTimeoutTestMessage(string server) { string Suc ...
- 【ACM】Fighting for HDU
#include <stdio.h> #include <stdlib.h> #define max 100 /* run this program using the con ...
- 【Spark】RDD操作具体解释2——值型Transformation算子
处理数据类型为Value型的Transformation算子能够依据RDD变换算子的输入分区与输出分区关系分为下面几种类型: 1)输入分区与输出分区一对一型 2)输入分区与输出分区多对一型 3)输入分 ...
- [置顶] 原创鼠标拖动实现DIV排序
先上效果图: 对比传统的排序,这是一个很不错的尝试,希望对大家有启发. 大家可以参考我的上一篇博文:http://blog.csdn.net/littlebo01/article/details/12 ...
- UITableView Scroll to top 手动设置tableview 滚动到 顶部
UITableView Scroll to top 手动设置tableview 滚动到 顶部 [mainTableView scrollRectToVisible:CGRectMake(0,0,1,1 ...
- ES6 Reflect
1.Reflect概述 ES6 为了操作对象而提供的新 API 2.Reflect设计目的 (1)将Object对象的一些明显属于语言内部的方法(比如Object.defineProperty),放到 ...
- Visual studio之C# 新建线程与定时器的使用
背景 App需要开线程和定时器,本文对这两种的通用方法做个记录 正文 线程 添加命名空间 using System.Threading; 创建线程 //shutdownlazer()即是线程要执行的函 ...
- mongodb的基本语法(二)
一.聚集集合查询 1.查询所有记录 db.userInfo.find(); 相当于:select* from userInfo; 默认每页显示20条记录,当显示不下的情况下,可以用it迭代命令查询下一 ...