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. Mysql:too many connect

    1.问题展现应用端登录出现Too many connections报错 检查发现mysql数据库服务端已经达到了max_connections上限 #查看最大连接数 mysql> show va ...

  2. (八)Spring 事务管理

    目录 文章目录 @[toc] **`Spring`** 事务管理 `Api` 介绍之 **`PlatformTransactionManager`** 后记 #Spring 的事务管理 编程式事务管理 ...

  3. vc++6.0中查看函数栈的结构

    栈:一种后进先出的数据结构   比如:弹夹 函数调用的约定 传参顺序 传参媒介 如何传递返回值 平衡参数(堆栈平衡):有且只有被调方(callee)和调用方(caller)一方执行 _cdell (c ...

  4. MongoDB环境搭建

    MongoDB系列第一课:MongDB简介 MongoDB系列第二课:MongDB环境搭建 MongoDB系列第三课:MongDB用户管理 MongoDB系列第四课:MongoDB数据库.集合.文档的 ...

  5. 【背包问题】PACKING

    题目描述 It was bound to happen.  Modernisation has reached the North Pole.  Faced with escalating costs ...

  6. Python 异常处理与反射机制

    Python 的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承.Py ...

  7. 植物大战僵尸:查找植物叠加种植CALL

    实验目标:我们都知道植物大战僵尸游戏中植物是不可以叠加种植的,也就是一个格子只能种植一个植物,今天我们将实现一个格子里种植无限多的植物. 我们首先需要找到植物的种植CALL,然后在逐步测试观察功能之间 ...

  8. MySQL create table语法中的key与index的区别

    在create table的语句中,key和index混淆在一起,官方手册中的解释是这样: KEY is normally a synonym for INDEX. The key attribute ...

  9. JS基础_枚举对象中的属性

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. PHP 手机短信发送验证码

    点击链接加入群[php/web 学习课堂]:https://jq.qq.com/?_wv=1027&k=5645xiw 欢迎大家加入,一起讨论学习 本篇设计的知识点有点多,我会分类将,同学们可 ...