javascript - TypeError:无法读取未定义的属性“clientWidth”
问题描述
当我将鼠标悬停在heading
. 我想,这是因为我用useRef()
错了。任何想法如何解决它?
import React, { useState, useRef } from 'react'; //
function Main (
const refs = useRef()
const onMouseMove = () => {
const width = refs.heading.clientWidth
const height = refs.heading.clientHeight
console.log(width,height);
}
return (
<div className='home__main-text-wrapper' >
<h1 className='heading'
onMouseMove={onMouseMove}
ref={refs}>
Get your pet <br /> a perfect ID photo
</h1>
</div>
)
)
解决方案
您需要访问current
.ref
const width = refs.current.clientWidth
const height = refs.current.clientHeight
有关更多详细信息,请阅读文档 - https://reactjs.org/docs/hooks-reference.html#useref
推荐阅读
- arangodb - 按字段合并文档
- ssl - SSL 证书错误如何仅针对非 www 域显示?
- java - java.lang.IllegalAccessError:从 vertx 访问 GraphQL API 时
- sql - 如何在sql中创建图表以按期间获取客户的帐户数
- python - 如何在 Python 中将输入函数与烧瓶一起使用,以捕获用户输入,然后根据该响应发送响应。像对话流
- python - 使用 Python API 从另一个网络连接到盈透证券交易平台
- chef-infra - 如何为厨师 remote_file 资源设置 http_proxy
- ios - 验证我的应用程序时出现证书问题
- stored-procedures - 存储过程中的原子插入
- c# - 更改现有 PDF 中的元素颜色