[React] Create a Persistent Reference to a Value Using React useRef Hook
The useRef is a hook for creating values that persist across renders. In this lesson we'll learn how to use the useRef hook to measure the width of an element as it changes.
import React, { useState, useEffect, useRef } from "react";
import ReactDOM from "react-dom";
import "./styles.css";
function useInput(defaultValue) {
const [value, setValue] = useState(defaultValue)
function onChange(e) {
setValue(e.target.value)
}
return {
value,
onChange
}
}
function App() {
const input = useInput("");
const messageRef = useRef();
useEffect(() => {
const boundingBox = messageRef.current.getBoundingClientRect();
console.log(boundingBox.width);
});
return (
<div className="App">
<h1>How to useRef in React</h1>
<input {...input} />
<div>
<span ref={messageRef}>{input.value}</span>
</div>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
[React] Create a Persistent Reference to a Value Using React useRef Hook的更多相关文章
- [React] Forward a DOM reference to another Component using forwardRef in React 16.3
The function forwardRef allows us to extract a ref and pass it to its descendants. This is a powerfu ...
- react系列笔记1 用npx npm命令创建react app
react系列笔记1 用npx npm命令创建react app create-react-app my-app是开始构建新的 React 单页应用程序的最佳方式.它已经为你设置好了开发环境,以便您可 ...
- 关于React.PropTypes的废除,以及新版本下的react的验证方式
React.PropTypes是React用来typechecking的一个属性.要在组件的props上运行typechecking,可以分配特殊的propTypes属性: class Greetin ...
- react全家桶从0搭建一个完整的react项目(react-router4、redux、redux-saga)
react全家桶从0到1(最新) 本文从零开始,逐步讲解如何用react全家桶搭建一个完整的react项目.文中针对react.webpack.babel.react-route.redux.redu ...
- React实战教程之从零开始手把手教你使用 React 最新特性Hooks API 打造一款计算机知识测验App
项目演示地址 项目演示地址 项目代码结构 前言 React 框架的优雅不言而喻,组件化的编程思想使得React框架开发的项目代码简洁,易懂,但早期 React 类组件的写法略显繁琐.React Hoo ...
- [React] Create and import React components with Markdown using MDXC
In this lesson I demonstrate how to use the library MDXC to create and import React components with ...
- [React] Create component variations in React with styled-components and "extend"
In this lesson, we extend the styles of a base button component to create multiple variations of but ...
- [React] Create & Deploy a Universal React App using Zeit Next
In this lesson, we'll use next to create a universal React application with no configuration. We'll ...
- [React] Create an Animate Content Placeholder for Loading State in React
We will create animated Content Placeholder as React component just like Facebook has when you load ...
随机推荐
- [转帖]2019-03-26 发布 深入理解 MySQL ——锁、事务与并发控制
深入理解 MySQL ——锁.事务与并发控制 https://segmentfault.com/a/1190000018658828 太长了 没看完.. 数据库 并发 mysql 639 次阅读 ...
- win7 配置 用户/系统DSN (解决plsql odbc导入器 DSN 没有选项)
(2013-05-07 15:07:29) 转载▼ 标签: it 老笔记本越来越卡,最近重新做了win7的系统 从sql server中导出的 excel文件 准备通过plsql倒入的oracle ...
- Springboot项目自动加载设置
SpringBoot是允许项目自动加载的,但是需要在pom文件映入依赖库 1.导入依赖库 <dependency> <groupId>org.springframework.b ...
- IDEA操作之test case coverage的方法
作用: 用于自动化测试,检查单元测试的覆盖率情况. 安装: 1.点击 Run * with coverage 或者右键已经定义为test source的package选择(单个test class同 ...
- 用JavaScript写一个简单的倒计时,可以应用在发送短信验证码的“59秒后重新发送验证短信”
倒计时——从10倒数到0,点击按钮会还原倒计时 <body> <!-- 将textvalue值设为10,从10倒数 --> <input type="text& ...
- 数据库设计规范、E-R图、模型图
(1)数据库设计的优劣: 糟糕的数据库设计: ①数据冗余冗余.存储空间浪费. ②数据更新和插入异常. ③程序性能差. 良好的数据库设计 ①节省数据的存储空间. ②能够保证数据的完整新. ③方便进行数据 ...
- 简单分析synchronized不会锁泄漏的原因
最近看到一句话:内部锁synchronized不会造成锁泄漏(Lock Leak). 锁泄漏是指一个线程获得某个锁以后,由于程序的错误.缺陷致使该锁一直没法被释放而导致其他线程一直无法获得该锁的现象. ...
- Typeof() 和 GetType()区别
1.typeof(x)中的x,必须是具体的类名.类型名称等,不可以是变量名称. 2.GetType()方法继承自Object,所以C#中任何对象都具有GetType()方法,它的作用和typeof() ...
- Linux常用命令(自用)
1 抓包 tcpdump port 5060 and host 192.168.1.180 tcpdump -i ethx -w 1.pcap -s 0 2. 查看硬盘使用情况 df ./ 3.查看进 ...
- 关于Highcharts数据量超过1000时无法显示问题
今天在vue的项目中引入Highcharts,想做一个大数据量的实时刷新曲线图,发现当数据量超过1000就无法显示. 经过排查发现 Highcharts为了保证更好的性能设置了一个性能阈值检查,当数据 ...