1

如何在单击时更改图标的颜色并且单击后颜色保持不变。它不会改变他们的状态。

  import {RiHeart3Fill} from 'react-icons/ri';
  import './Details.scss';




  <div className="details__info">
                        <div className="details__incDec">
                            <span className="dec" onClick={decQuantity}><BsDash /></span>
                            <span className="quantity">{quantity}</span>
                            <span className="inc" onClick={() => setQuantity(quantity + 1)}><BsPlus /></span>
                           {localStorage.getItem('email') 
                            ? <button  style={{outline: 'none',fontFamily: "'Comfortaa', cursive"}} className="btn-default"  onClick={cartData}>add to cart</button>
                            : <button  style={{outline: 'none',fontFamily: "'Comfortaa', cursive"}} className="btn-default" onClick={signUpToBuy}>add to cart</button>
                            }
                            {localStorage.getItem('email') 
                            ?   <RiHeart3Fill className="heart"/>
                            :   <RiHeart3Fill className="heart"/>
                            }
                       </div>
  </div>

详细信息.scss

.heart{
   font-size: 35px;
   color:rgb(182, 173, 173);
   margin-top: 7px;
    width: 70px;
    outline: none;
    text-transform: uppercase;
    cursor: pointer;
    font-weight: bold;
    &:hover{
        color: rgb(192, 39, 39);
    }
}
4

2 回答 2

0

如果您使用的是功能组件,那么您可以使用useState

 const [iconColor,setIconColor] = useState("white");

您可以执行以下操作:


  <div className="details__info">
                        <div className="details__incDec">
                            <span className="dec" onClick={decQuantity}><BsDash /></span>
                            <span className="quantity">{quantity}</span>
                            <span className="inc" onClick={() => setQuantity(quantity + 1)}><BsPlus /></span>
                           {localStorage.getItem('email') 
                            ? <button  style={{outline: 'none',fontFamily: "'Comfortaa', cursive"}} className="btn-default"  onClick={cartData}>add to cart</button>
                            : <button  style={{outline: 'none',fontFamily: "'Comfortaa', cursive"}} className="btn-default" onClick={signUpToBuy}>add to cart</button>
                            }
                            {localStorage.getItem('email') 
                            ?   <RiHeart3Fill className="heart"/>
                            :   <RiHeart3Fill  className="heart" style={{color:iconColor}} onClick={()=>setIconColor("red")}/>
                            }
                       </div>
  </div>

于 2021-01-27T05:25:51.750 回答
0

首先,对于 Icons 条件,如果您的条件为真或假,则您拥有它,因此条件是多余的

  <RiHeart3Fill className="heart"/>

然后,如果您想更改图标颜色,则基本上需要创建一个函数来修改状态,如下所示:您正在使用类组件,它将是这样的:

constructor(props) {
    super(props);
    this.state = {
  toggleHeart = false
    };
this.changeColor= this.changeCoor.bind(this);
  }
    changeColor = () =>{
     this.setState({toggleHeart: !toggleHeart})
    }`enter code here`
    <RiHeart3Fill className={
            this.state.toggleHeart ? 'heart active' : 'heart'
          } onClick={changeColor}/>

对于功能组件,它几乎是相似的:

const  [toggleHeart, setToggleHeart] = useState(false)
    
    changeColor = useCallback(() =>{
     setToggleHeart(!toggleHeart)
    },[])
    <RiHeart3Fill className={
            toggleHeart ? 'heart active' : 'heart'
          } onClick={changeColor}/>

在 scss 文件中,您将拥有如下内容:

.heart{
   font-size: 35px;
   color:rgb(182, 173, 173);
   margin-top: 7px;
    width: 70px;
    outline: none;
    text-transform: uppercase;
    cursor: pointer;
    font-weight: bold;
    &:hover{
        color: rgb(192, 39, 39);
    }
   &.active {
    color: ///color when active
   }
}
于 2021-01-26T22:08:56.860 回答