React文档(十六)refs和DOM
在标准的React数据流中,props是使得父组件和子组件之间交互的唯一方式。你通过props重新渲染子组件。然而,有些情况需要你必须在数据流之外修改一个子组件。这个子组件可以是一个React组件的实例,或者是一个DOM元素。对于这两种情况,React提供了解决方式。
什么时候使用refs
以下有几种合适的情况使用refs:
- 处理焦点,文本选择或者媒体播放
- 触发强制的动画
- 整合第三方DOM库
避免使用refs在可以声明式地解决的东西上。
举个例子,不使用open()和close()方法在Dialog组件上,而是传递一个isOpen属性给它。
- class MyComponent extends React.Component {
- constructor(props) {
- super(props);
- this.myRef = React.createRef();
- }
- render() {
- return <div ref={this.myRef} />;
- }
- }
- const node = this.myRef.current;
- class CustomTextInput extends React.Component {
- constructor(props) {
- super(props);
- // 创建 ref 存储 textInput DOM 元素
- this.textInput = React.createRef();
- this.focusTextInput = this.focusTextInput.bind(this);
- }
- focusTextInput() {
- // 直接使用原生 API 使 text 输入框获得焦点
- // 注意:通过 "current" 取得 DOM 节点
- this.textInput.current.focus();
- }
- render() {
- // 告诉 React 我们想把 <input> ref 关联到构造器里创建的 `textInput` 上
- return (
- <div>
- <input
- type="text"
- ref={this.textInput}} />
- <input
- type="button"
- value="Focus the text input"
- onClick={this.focusTextInput}
- />
- </div>
- );
- }
- }
- class AutoFocusTextInput extends React.Component {
- constructor(props) {
- super(props);
- this.textInput = React.createRef();
- }
- componentDidMount() {
- this.textInput.current.focusTextInput();
- }
- render() {
- return (
- <CustomTextInput ref={this.textInput} />
- );
- }
- }
- class CustomTextInput extends React.Component {
- // ...
- }
- function MyFunctionalComponent() {
- return <input />;
- }
- class Parent extends React.Component {
- constructor(props) {
- super(props);
- this.textInput = React.createRef();
- }
- render() {
- // 这将 *不会* 工作!
- return (
- <MyFunctionalComponent ref={this.textInput} />
- );
- }
- }
- function CustomTextInput(props) {
- // 这里必须声明 textInput,这样 ref 回调才可以引用它
- let textInput = null;
- function handleClick() {
- textInput.focus();
- }
- return (
- <div>
- <input
- type="text"
- ref={(input) => { textInput = input; }} />
- <input
- type="button"
- value="Focus the text input"
- onClick={handleClick}
- />
- </div>
- );
- }
- class CustomTextInput extends React.Component {
- constructor(props) {
- super(props);
- this.textInput = null;
- this.setTextInputRef = element => {
- this.textInput = element;
- };
- this.focusTextInput = () => {
- // 直接使用原生 API 使 text 输入框获得焦点
- if (this.textInput) this.textInput.focus();
- };
- }
- componentDidMount() {
- // 渲染后文本框自动获得焦点
- this.focusTextInput();
- }
- render() {
- // 使用 `ref` 的回调将 text 输入框的 DOM 节点存储到 React
- // 实例上(比如 this.textInput)
- return (
- <div>
- <input
- type="text"
- ref={this.setTextInputRef}
- />
- <input
- type="button"
- value="Focus the text input"
- onClick={this.focusTextInput}
- />
- </div>
- );
- }
- }
- function CustomTextInput(props) {
- return (
- <div>
- <input ref={props.inputRef} />
- </div>
- );
- }
- class Parent extends React.Component {
- render() {
- return (
- <CustomTextInput
- inputRef={el => this.inputElement = el}
- />
- );
- }
- }
React文档(十六)refs和DOM的更多相关文章
- React文档(六)state和生命周期
想一下之前的章节时钟的例子. 目前为止我们只学习了一直方式去更新UI. 我们调用ReactDOM.render()方法去改变渲染的输出: function tick() { const element ...
- React文档(十三)思考React
在我们的看来,React是使用js创建大型快速网站应用的首要方法.它在Facebook和Instagram的使用已经为我们展现了它自己. React的一个很好的地方就在于当你创建应用的时候它使你思考如 ...
- React文档(二十四)高阶组件
高阶组件(HOC)是React里的高级技术为了应对重用组件的逻辑.HOCs本质上不是React API的一部分.它是从React的组合性质中显露出来的模式. 具体来说,一个高阶组件就是一个获取一个组件 ...
- react文档demo实现输入展示搜索结果列表
文档页面地址:https://doc.react-china.org/docs/thinking-in-react.html 该文档只给了具体实现思路,下面是我实现的代码. 初学react,如果有写的 ...
- React文档(一)安装
React是一个灵活的可以用于各种不同项目的框架,你可以用它来写新应用,你也可以逐步将它引进已有的代码库而不用重写整个项目. 试用React 如果你想玩一玩React,那么就去CodePen上试一试. ...
- python【第十六篇】DOM
文档对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展标志语言的标准编程接口. DOM可以以一种独立于平台和语言的方式访问和修改一个文档的内容和结构.换句 ...
- JAVA读取XML,JAVA读取XML文档,JAVA解析XML文档,JAVA与XML,XML文档解析(Document Object Model, DOM)
使用Document Object Model, DOM解析XML文档 也可参考我的新浪博客:http://blog.sina.com.cn/s/blog_43ac5543010190w3.html ...
- QT XML文档的解析 QXmlStreamReader, DOM,SAX 三种解析方法 简单示例
0. xml文档如下 <?xml version="1.0"?> <bookindex> <entry term="sidebearings ...
- Drools文档(六) 用户手册
用户手册 基础 无状态的知识Session Drools规则引擎拥有大量的用例和功能,我们要如何开始?你无须担心,这些复杂性是分层的,你可以用简单的用例来逐步入门. 无状态Session,无须使用推理 ...
随机推荐
- 安装mysql服务时提示“找不到msvcp140.dll”
没有安装VC++2015版运行库导致的(Microsoft Visual C++ 2015 Redistributable),下载地址https://www.microsoft.com/en-us/d ...
- websocket作用
1.即时通讯 web即时通讯(网页的QQ,聊天系统等)可以通过websocket实现. 2.轮询 web开发中,有时需要通过轮询(比如时间间隔5秒)去服务器读取数据. 使用HTTP协议向服务器发送re ...
- Mac快捷键大全及cheatsheet插件
常用快捷键 CheatSheet 在微信上看到一篇文章分享了一款小软件,觉得有必要推荐一下.名叫CheatSheet.如果你想知道某程序的所有快捷键,只需在Mac上安装CheatSheet,然后长按住 ...
- 017-并发编程-Condition
一.概述 任何一个Java对象,都拥有一组监视器方法,主要包括wait().notify().notifyAll()方法,这些方法与synchronized关键字配合使用可以实现等待/通知机制.使用这 ...
- centos7.5图形界面与命令行界面转换
查看当前状态下的显示模式: # systemctl get-default 转换为图形界面: # systemctl set-default graphical.target 转换为命令行界面: # ...
- 线上bug分析
昨天下午大神把组内几十号人召集在一起开Online bug分析大会,主要是针对近期线上事故从事故原因和解决方案两个维度来分析. 对金融软件来说,每一次的线上事故都有可能给公司带来重大的损失,少扣了用户 ...
- CentOS7查看systemctl 控制的服务的相关配置
例如,启动配置文件 [root@Docker_Machine_192.168.31.130 ~]# systemctl show --property=FragmentPath docker Frag ...
- Html h1-h6 标签
Html h1-h6 标签 <html> <body> <!-- h1-h6标签:标题标签 --> <h1>XSK</h1> <h2& ...
- 提取路由器固件中的squashfs
之前用binwalk -Me提取固件中的squashfs,但会生成大量的压缩包等,只提取文件系统的话,可以定位squashfs的文件头(hsqs)位置,然后用dd将它分离出来,然后在unsquashf ...
- [c/c++] programming之路(24)、字符串(五)——字符串插入,字符串转整数,删除字符,密码验证,注意事项
1.将字符串插入到某位置(原字符串“hello yincheng hello cpp hello linux”,查找cpp,找到后在cpp的后面插入字符串“hello c”) 需要用到strstr字符 ...