0

I want to change the icon on the button whenever it is clicked, The button has onClick event to add product to basket.

 <Button style={{ backgroundColor: "transparent" }} type="primary" icon={<img src={plusCart} />} onClick={addToBasket} /> 

3 Answers 3

2
const [buttonClicked, setButtonClicked] = useState(false); const addToBasket = () => { ... setButtonClicked(true); }; ... <Button ... icon={<img src={buttonClicked ? someIcon : plusCart} />} onClick={addToBasket} /> 
Sign up to request clarification or add additional context in comments.

6 Comments

If this works for you please click the checkmark (✓) to accept it. That way others know that you've been (sufficiently) helped. Also, see stackoverflow.com/help/someone-answers.
how can we do this for cursor hover, change icons of a button on hover in react ??
@tiedantebreak Remove onClick prop and try this props onMouseEnter={() => setButtonClicked(true)} onMouseLeave={() => setButtonClicked(false)}
@tiedantebreak Let me know if it works.
yes it worked thanks
|
1

You can use a boolean state for this, maybe like so:

const [buttonClicked, setButtonClicked] = useState(false); const addToBasket = () => { setButtonClicked(true); // your code.. }; <Button style={{ backgroundColor: "transparent" }} type="primary" icon={buttonClicked ? <img src={otherIcon}/> : <img src={plusCart}/>} onClick={addToBasket} /> 

2 Comments

how can we do this for cursor hover, change icons of a button on hover in react ?? –
1

You can store information if button was clicked in useState hook and onClick change this state:

const [clicked, setClicked] = React.useState(false) return( <Button style={{ backgroundColor: "transparent" }} type="primary" icon={<img src={clicked ? plusCart : minusCart} />} onClick={(e)=>{ setClicked(true); addToBasket(e);}} /> ) 

1 Comment

how can we do this for cursor hover, change icons of a button on hover in react ?? –

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.