[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 string ref pattern, callback refs, and then how to use the new createRef() method in React 16.3.
Additional Resources: refs and the dom
You can use 'React.createRef()' to create a ref object. Then to access it from <obj>.current.<ref_name>
import React from "react";
import { render } from "react-dom"; class App extends React.Component {
fullName = React.createRef(); handleBlur = () => {
this.fullName.current.blur();
}; handleFocus = () => {
this.fullName.current.focus();
}; render() {
return (
<div className="section">
<div className="field">
<label className="label">Full Name</label>
<div className="control">
<input className="input" ref={this.fullName} type="text" />
</div>
</div>
<button
className="button is-link is-outlined"
onClick={this.handleFocus}
>
Focus
</button>{" "}
<button
className="button is-danger is-outlined"
onClick={this.handleBlur}
>
Blur
</button>
</div>
);
}
} render(<App />, document.getElementById("root"));
[React] Reference a node using createRef() in React 16.3的更多相关文章
- 《React Native 精解与实战》书籍连载「Node.js 简介与 React Native 开发环境配置」
此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...
- React Native v0.4 发布,用 React 编写移动应用
React Native v0.4 发布,自从 React Native 开源以来,包括超过 12.5k stars,1000 commits,500 issues,380 pull requests ...
- React躬行记(5)——React和DOM
React实现了一套与浏览器无关的DOM系统,包括元素渲染.节点查询.事件处理等机制. 一.ReactDOM 自React v0.14开始,官方将与DOM相关的操作从React中剥离,组成单独的rea ...
- 【React】学习笔记(一)——React入门、面向组件编程、函数柯里化
课程原视频:https://www.bilibili.com/video/BV1wy4y1D7JT?p=2&spm_id_from=pageDriver 目录 一.React 概述 1.1.R ...
- React.js入门笔记(续):用React的方式来思考
本文主要内容来自React官方文档中的"Thinking React"部分,总结算是又一篇笔记.主要介绍使用React开发组件的官方思路.代码内容经笔者改写为较熟悉的ES5语法. ...
- 五分钟学习React(三):纯HTML代码搭建React应用
上一期我们使用了React官方的脚手架运行React应用.大家可能会觉得这种方法很繁琐,需要配置各种第三方插件.JQuery时代的前端真是让人怀念.这一期,我就带领大家创建一个"怀旧版&qu ...
- React 源码剖析系列 - 不可思议的 react diff
简单点的重复利用已有的dom和其他REACT性能快的原理. key的作用和虚拟节点 目前,前端领域中 React 势头正盛,使用者众多却少有能够深入剖析内部实现机制和原理. 本系列文章希望通过剖析 ...
- 【React】383- React Fiber:深入理解 React reconciliation 算法
作者:Maxim Koretskyi 译文:Leiy https://indepth.dev/inside-fiber-in-depth-overview-of-the-new-reconciliat ...
- 没有用到React,为什么我需要import引入React?
没有用到React,为什么我需要import引入React? 本质上来说JSX是React.createElement(component, props, ...children)方法的语法糖. 所以 ...
随机推荐
- 获取android的SDK或者手机目录路径
获取android的SDK或者手机目录路径 Google为我们提供了API来获取SDK或者手机目录路径: 1.获取SD卡目录 File file1 = Environment.getExternalS ...
- 【SDOI 2010】 计算器
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2242 [算法] 第一问用快速幂解决 第二问用exgcd解决 第三问用BSGS算法解决 ...
- ettercap + driftnet 实现同网段下流量欺骗
前言: 由于在局域网中,网关会不断地发送 ARP 数据包询问当前是否有新的客户端上线,如果我们可以欺骗当前局域网网段下的主机, 把我们当成网关地址,并且我们把欺骗的流量转发到真正的网关地址,这样我们就 ...
- JS排序之冒泡排序
冒泡排序的两种策略: <script>// 第一种思路:// 一个数组中的数据,拿第一个和剩下的依次进行对比,数值小的赋值给第一个,一轮比较过后,则数值小的放在最前边.// 第二轮比较,则 ...
- express搭建一个web服务器
npm install express -g express这个库可以使用了. npm install express-generator -g ...
- (转载) popupWindow 指定位置上的显示
popupWindow 指定位置上的显示 标签: androidpopupWindowpopupWindow具体位置放置 2014-07-09 16:23 1114人阅读 评论(0) 收藏 举报 分 ...
- 在无任何报错的情况下 pagehelper.startpage分页无效问题
问题原因:自从spring boot开始使用2.0x版本以上后,很多相应的依赖文件版本开始变化 该版本为spring-boot 1.4.1 <dependency> <groupId ...
- Py基础+中级
原文 Py学习博客 1:https://www.cnblogs.com/fu-yong/p/8060198.html2:while True:just do it 三.Python的默认编码 ▷pyt ...
- AI:从游戏引擎--到AI
原文链接:http://blog.csdn.net/left_la/article/details/6358911#t9 这是我在Gameres上看到的一篇文章,文章很长,全文分为11个部分,看后感觉 ...
- PhotoZoom Classic 7中的新功能
众所周知PhotoZoom Classic是家庭使用理想的放大图像软件.目前很多用户还在使用PhotoZoom Classic 6,对于PhotoZoom Classic 7还是有点陌生.其实在6代衍 ...