博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
react useref_如何使用useRef React钩子
阅读量:2503 次
发布时间:2019-05-11

本文共 1168 字,大约阅读时间需要 3 分钟。

react useref

Check out my first, if you’re new to them.

如果您是新手,请先查看我的 。

One React hook I sometimes use is useRef.

我有时使用的一个React挂钩是useRef

import React, { useRef } from 'react'

This hook allows us to access a DOM element imperatively.

这个钩子允许我们强制性地访问DOM元素。

Here’s an example, where I log to the console the value of the DOM reference of the span element that contains the count value:

这是一个示例,其中我将包含计数值的span元素的DOM引用的值登录到控制台:

import React, { useState, useRef } from 'react'const Counter = () => {  const [count, setCount] = useState(0)  const counterEl = useRef(null)  const increment = () => {    setCount(count + 1)    console.log(counterEl)  }  return (    <>      Count: {count}            )}ReactDOM.render(
, document.getElementById('app'))

Notice the const counterEl = useRef(null) line, and the <span ref={counterEl}>{count}</span>. This is what sets the link.

注意const counterEl = useRef(null)行,以及<span ref={counterEl}>{count}</span> 。 这就是设置链接的原因。

Now we can access the DOM reference by accessing counterEl.current.

现在,我们可以通过访问counterEl.current来访问DOM引用。

See it on Codepen:

在Codepen上查看:

See the Pen by Flavio Copes () on .

见笔由弗拉维奥·科佩斯( 上) 。

翻译自:

react useref

转载地址:http://cmqgb.baihongyu.com/

你可能感兴趣的文章
Java设计模式之《模板模式》及使用场景
查看>>
Linux编程入门
查看>>
坑爹的箭头函数
查看>>
Python 环境搭建
查看>>
IOS 在不打开电话服务的时候,可以响应服务器的推送消息,从而接收服务器的推送消息...
查看>>
置顶的博客
查看>>
ionic2 native app 更新用户头像暨文件操作
查看>>
SQL Server R2 地图报表制作(一)
查看>>
ZeroMQ——一个轻量级的消息通信组件
查看>>
JavaScript中数组和json的复制
查看>>
新概念4-30
查看>>
新概念4-40
查看>>
Android开发之Handler
查看>>
C语言多线程编程二
查看>>
转载:从集群计算到云计算
查看>>
服务器文件管理
查看>>
JavaScript基本整理3
查看>>
作业2
查看>>
ios上架报错90080,90087,90209,90125 解决办法
查看>>
给button添加UAC的小盾牌图标
查看>>