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. [转帖]NTLM说明

    [思路/技术]Windows认证 | 网络认证     来源:https://bbs.ichunqiu.com/thread-53598-1-1.html   圣乱X无心i春秋-核心白帽 发表于 5  ...

  2. 关于Android的资源id

    1 @+id/xx 这个表示向资源文件中添加一个新的id. @+id是在R文件中生成int xxx=value. 有两种情况 ①R文件中不存在xxx变量,则生成int xxx=value即为控件新建一 ...

  3. 大话数据结构(8) 串的模式匹配算法(朴素、KMP、改进算法)

    --喜欢记得关注我哟[shoshana]-- 目录 1.朴素的模式匹配算法2.KMP模式匹配算法 2.1 KMP模式匹配算法的主体思路 2.2 next[]的定义与求解 2.3 KMP完整代码 2.4 ...

  4. 2.1spring cloud 环境配置

    前提:SpringBoot可以离开SpringCloud独立使用开发项目,但是SpringCloud离不开SpringBoot,属于依赖的关系. 所以基本是搭建SpringBoot + 组件 = Sp ...

  5. S02_CH06_XADC实验

    S02_CH06_XADC实验 6.1实验概述 这次借助zynq的内嵌的XADC来采集zynq内部的一些参数: •VCCINT:内部PL核心电压 •VCCAUX:辅助PL电压 •VREFP:XADC正 ...

  6. varnish 子程序流程

    VCL中主要动作: pass:当一个请求被pass后,这个请求将通过varnish转发到后端服务器,该请求不会被缓存,后续的请求仍然通过Varnish处理.pass可以放在vcl_recv 和vcl_ ...

  7. java都13了, 8的新特性你还没不会用吗

    前言 java13都已经来了,很多同学还停留在使用java5的东西.如果在日常开发中没有使用上java8的一些新特性或者不会用.这篇文章对你可能有帮助. lambda表达式 介绍 lambda表达式是 ...

  8. CentOS7 mysql支持中文

    # vim /etc/my.cnf # For advice on how to change settings please see# http://dev.mysql.com/doc/refman ...

  9. net core体系-Xamarin-2概要(lignshi)

    通过本套课程的学习,各位学员能够对Xamarin有一个比较清楚的认识,掌握Xamarin常用功能的使用方法,能够比较熟练的使用Xamarin进行App(移动应用)的开发,能够比较轻松.快速地投入项目当 ...

  10. redis 交集、并集、差集

    sinter .sunion .sdiff redis 支持 Set集合的数据存储,其中有三个比较特殊的方法: sinter key [key …] 返回一个集合的全部成员,该集合是所有给定集合的交集 ...