简体   繁体   English

在从 React Icons 导入的图标上使用 useRef

[英]Using useRef on imported Icons from React Icons

im just trying to set a Ref to an imported React Icon.我只是想将 Ref 设置为导入的 React 图标。 But for some reason it does not work.但由于某种原因它不起作用。 I receive the following error message:我收到以下错误消息:

"TypeError: Cannot read properties of undefined (reading 'style')" “类型错误:无法读取未定义的属性(读取‘样式’)”

Is there a special way to refer to an imported file?有没有一种特殊的方式来引用导入的文件?

 import React from 'react' import { useRef } from 'react' import {HiPlus, HiThumbDown, HiThumbUp, HiArrowNarrowRight} from "react-icons/hi" function Card(props) { var addToFavRef = useRef() var thumbUpRef = useRef() var thumbDownRef = useRef() function addToFavorites(){ addToFavRef.current.style.color = "orange" addToFavRef.current.style.transform = "rotate(45deg)" } function thumbUp(){ thumbDownRef.current.style.color = "grey" thumbDownRef.current.style.opacity = "50%" } return ( <> <HiPlus className="cardPlusIcon" ref={addToFavRef}></HiPlus> <HiThumbUp className="cardThumbUp" ref={thumbUpRef} onClick={thumbUp}></HiThumbUp> <HiThumbDown className="cardThumbDown" ref={thumbDownRef} onClick={thumpDown}></HiThumbDown> </> ) } export default Card

Here's the message:这是消息:

在此处输入图片说明

I don't believe that there is a ref attached to the react icons svg.我不相信反应图标 svg 上有参考。 If you use typescript you can see the error when you try to add one.如果您使用打字稿,则在尝试添加时会看到错误。

If you really must access the dom node then you can wrap each icon in a div or span and attach a ref to that and then you can access it by the child node like ref.current.childNodes[0]如果你真的必须访问 dom 节点,那么你可以将每个图标包装在一个 div 或 span 中并附加一个引用,然后你可以通过像ref.current.childNodes[0]这样的子节点访问它

But it makes no sense in your case to do this if you are going to set the style just set it.但是,如果您要设置样式只是设置它,那么在您的情况下这样做是没有意义的。 There is no reason to access the dom node here.这里没有理由访问dom节点。 You can do so by adding classes or just setting the style directly.您可以通过添加类或直接设置样式来实现。

import {HiPlus, HiThumbDown, HiThumbUp, HiArrowNarrowRight} from "react-icons/hi"
import {useState} from 'react'

const thumbsUpStyle = {
  color: 'orange',
  transform: 'rotate(45deg)'
}

const thumbsDownStyle = {
  color: 'orange',
  transform: 'rotate(-45deg)'
}

const favoritesStyle = {
  color: 'grey',
  opacity: '50%'
}

export default function Card() {
  const [thumbs, setThumbs] = useState('')
  const [favorites, setFavorites] = useState(false)

  return (
    <div className="card">
      <HiPlus onClick={() => setFavorites(true)} style={favorites && favoritesStyle}/>
      <HiThumbUp onClick={() => setThumbs('up')} style={thumbs === 'up' && thumbsUpStyle}/>
      <HiThumbDown  onClick={() => setThumbs('down')} style={thumbs === 'down' && thumbsDownStyle}/>
    </div>
  );
}

Or just add a corresponding class and style that class in your stylesheet或者只是在样式表中添加相应的类和样式

import {HiPlus, HiThumbDown, HiThumbUp, HiArrowNarrowRight} from "react-icons/hi"
import {useState} from 'react'

export default function Card() {
  const [thumbs, setThumbs] = useState('')
  const [favorites, setFavorites] = useState(false)

  return (
    <div className="card">
      <HiPlus onClick={() => setFavorites(true)} className={favorites && 'favorites-class'}/>
      <HiThumbUp onClick={() => setThumbs('up')} className={thumbs === 'up' && 'thumbs-up-class'}/>
      <HiThumbDown  onClick={() => setThumbs('down')} className={thumbs === 'down' && 'thumbs-down-class'}/>
    </div>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM