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的更多相关文章

  1. [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 ...

  2. react系列笔记1 用npx npm命令创建react app

    react系列笔记1 用npx npm命令创建react app create-react-app my-app是开始构建新的 React 单页应用程序的最佳方式.它已经为你设置好了开发环境,以便您可 ...

  3. 关于React.PropTypes的废除,以及新版本下的react的验证方式

    React.PropTypes是React用来typechecking的一个属性.要在组件的props上运行typechecking,可以分配特殊的propTypes属性: class Greetin ...

  4. react全家桶从0搭建一个完整的react项目(react-router4、redux、redux-saga)

    react全家桶从0到1(最新) 本文从零开始,逐步讲解如何用react全家桶搭建一个完整的react项目.文中针对react.webpack.babel.react-route.redux.redu ...

  5. React实战教程之从零开始手把手教你使用 React 最新特性Hooks API 打造一款计算机知识测验App

    项目演示地址 项目演示地址 项目代码结构 前言 React 框架的优雅不言而喻,组件化的编程思想使得React框架开发的项目代码简洁,易懂,但早期 React 类组件的写法略显繁琐.React Hoo ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. [转帖]中国新超算彻底告别进口CPU 国产芯片已可与国外抗衡

    中国新超算彻底告别进口CPU 国产芯片已可与国外抗衡 蓝天·2017-10-17·本土IC 来源: 观察者网 https://www.laoyaoba.com/html/news/newsdetail ...

  2. Django使用DataTables插件总结

    Django使用Datatables插件总结 文章中的例子已上传至github 基本使用 Datatables插件是一款方便简单的展示数据的列表插件.关于基本使用,官方网站上的已介绍的很详细,这里我再 ...

  3. 在linux下进行数据备份

    一.完全备份 完全备份是指把所有需要备份的数据全部备份.当然,完全备份可以备份整块硬盘.整个分区或某个具体的目录.完全备份的好处是数据恢复方便,因为所有的数据都在同一个备份中,所以只要恢复完全备份,所 ...

  4. ARM-常见考题和知识点

    1. ARMv7 7中状态,ARMv8对应的状态 2. TEE知识 3. ARM寄存器及作用 4. ARM内部总线AHB APB 5. 1. Thumb | Arm指令区别 编写Thumb指令时,先要 ...

  5. centos7.2 安装Lnmp

    1. 安装编译工具及库文件 yum install -y make apr* autoconf automake curl  \ curl-devel gcc gcc-c++ cmake gtk+-d ...

  6. 作业3:java对象模型

    一 对象表示机制 1 Hotsplot JVM内部对象表示系统 (1)OOP-Klass二分模型 OOP:Ordinary Object Pointer 或者OOPS.即普通对象指针,描述对象实例信息 ...

  7. grpc的demo

    一 grpc的为什么比http快? 1- 用Proto Buffer 作为序列化工具 2- 采用http2协议,头部压缩,多路复用 3- 基于netty的IO框架 二 grpc的demo A lib工 ...

  8. javaIO——AutoCloseable 小试

    前面在 IO 概述篇提到过,AutoCloseable 接口类会自动调用 close() 方法,那究竟具体怎么写呢?以及发生异常情况下或者多个资源是不是都能自动调用呢?我们来写一个简单的类测试一下就知 ...

  9. IOS 点击按钮拨号

    - (IBAction)OnTouch_bHotLine:(id)sender { [[UIApplication sharedApplication] openURL:[NSURL URLWithS ...

  10. 行内块和文字垂直对齐vertical-agign

    vertical-align 垂直对齐 (对于块级元素无效,主要用来控制表单或者图片与文字对齐的) 图片和文字默认是基线对齐 属性: baseline 基线 top 顶线 middle 中线 bott ...