[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 ...
随机推荐
- Redis 学习笔记(篇五):对象(RedisObject)
Redis-对象 在以前的文章中,我们介绍了 Redis 用到的主要数据结构,比如简单动态字符串.双端链表.字典.压缩列表.整数集合. 然而 Redis 并没有直接使用这些数据结构来实现键值对的数据库 ...
- Photon Server初识(三) ---ORM映射改进
一:新建一些管理类, 二.实现每个管理类 (1)NHibernateHelper.cs 类,管理数据库连接 using NHibernate; using NHibernate.Cfg; namesp ...
- git 使用2
安装 1.下载对应版本:https://git-scm.com/download 2.安装git:在选取安装路径的下一步选取 Use a TrueType font in all console wi ...
- SHE姐妹建模记录
中午11点54分,队长把MD5码提交上去在群里发了截图,我对着屏幕上刚检查完的论文,感觉整个人都轻松起来了,又有点恍惚,可能是这几天都没睡好觉.去楼下吃了顿饭,本来打算回来倒头就睡,睡到几点算几点,醒 ...
- gulp做简单的js压缩
安装工具 全局安装npm install --global gulp 项目的依赖安装npm install --save-dev gulp npm install --save-dev gulp-re ...
- visual studio 用 vs code 的 hot key
记得 2 年多前开始用 vs code, 一开始非常不适应它的 hot key 一心想把 vs code 的 hot key 全改成 visual studio 的,但一直没有找到比较方便的办法 (总 ...
- winfrom_动态添加按钮button(设置颜色,大小,按钮字体大小、颜色,位置,事件)
List<string> strColor = new List<string>(); strColor.Add("#e67817"); strColor. ...
- plsql developer字符集和oracle字符集不一致的解决方法(转)
如果oracle安装选择的字符集与PLSQL Developer的字符集不同,则打开 PLSQL Developer时会提示字符不一致可能造成问题: 定位到注册表的以下位置: HKEY_LOCAL_M ...
- SIP 3pcc
3PCC全称Third Party Call Control,中文即第三方电话呼叫控制,指的是由第三方控制者在另外两者之间建立一个会话,由控制者负责会话双方的媒体协商.3PCC是一种非常灵活的会话控制 ...
- bootsctrap4 datepicker时间选择插件
现在网上基本都是v3的时间选择插件,花了点时间改了找了个v4能用的 bootstrap-datepicker <!DOCTYPE html> <html> <head&g ...