1

How can I change the color of my icon when it's clicked and the color remains the same after clicking. It will not change their state.

  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>

Details.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);
    }
}
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Zain Zahid
  • 33
  • 2
  • 8

2 Answers2

0

first, for the Icons condition, you have it if your condition is true or false so having the condition is redundant

  <RiHeart3Fill className="heart"/>

Then if you wanted to change the icon color you would basically need to create a function to modify the state like the following if: you are using a class component it would be like this:

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}/>

for a functional component, it will be almost similar:

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

and in the scss file you will have some thing like this:

.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
   }
}
Souad
  • 51
  • 3
0

if you'r using functional component then you can use useState

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

you can do something like:


  <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>

chaitanya
  • 186
  • 1
  • 1
  • 8