[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)方法的语法糖. 所以 ...
随机推荐
- Linux - 控制台界面,虚拟界面,字符界面
tty控制台终端. pts虚拟终端. tty1 图形界面. tty2 字符界面. Ctrl+Alt+F2-6 在字符界面下,通过Alt+F2 切换回来.或者切换到其他的字符界面. Alt+F2 pts ...
- Gym-101915J The Volcano Eruption 计算几何
题面 题意:给你一个矩阵,然后有很多的圆,这些圆可能相交着,一个或者几个就导致这个矩形被分割开了,就是从最下面的边到上面的边,连线被这些圆阻隔了,每一堆圆当做一个阻碍,问一共有几个阻碍 题解:看起来好 ...
- Git Learning Part I - Install Git and configure it
Why we need 'Git' GIt version control: 1. record the history about updating code and deleting code 2 ...
- 【转载】程序猿转型AI必须知道的几件事!
历史上AI火过两次,但是最终都已销声匿迹作为结束.这次AI大火的原因:AlphaGo 4比1战胜李世石,相对于一些外行人的恐慌和恐惧,其实很多业内人员在这场世纪之战结束后,都为人类点上了一个大大的赞. ...
- Appium 环境搭建 - macOS
本文没有安装 Appium Desktop,Appium Server 直接在命令行中进行即可. Homebrew,macOS 包管理器: ruby -e "$(curl -fsSL htt ...
- 【Oracle】ORA-55610: Invalid DDL statement on history-tracked table
—删除表emp1时出现问题 SCOTT@GOOD> drop table emp1; drop table emp1 * ERROR at line 1: ORA-55610: Invalid ...
- vue首次进入微信没有标题问题
首先实在路由改变的时候可以有标题的 首次进入路由不显示标题,查到很多,最有解决可以自定义标签, 后者引入一个包vue-wechat-title,我就是用的后者,前面的没有式过 上地址 http ...
- 服务器控件使用eval()绑定属性出现服务器标记的格式不正确
在使用asp.net服务器端控件的时候,想要动态绑定控件某属性的值,或者动态绑定控件事件方法的参数,例如一个<asp:RadioButton ID="RadioButton5" ...
- 组装自己的tesla超级计算机
原文链接:blog.csdn.net/xqj198404/article/details/20016279 NVIDIA链接:http://www.nvidia.cn/object/tesla_bui ...
- Postfix Self Expression
Postfix Self Expression A postfix self expression consists of an expression or the name of a type, i ...