tiedantebreak
tiedantebreak

Reputation: 89

How to use useEffect to change icon of a button on cursor hover in react

How to change the button icon on mouse hover with useEffect?

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

Upvotes: 0

Views: 1122

Answers (1)

Taghi Khavari
Taghi Khavari

Reputation: 6582

You can change your code to something like this:

const [icon, setIcon] = useState(plusCart)
<Button
  style={{ backgroundColor: "transparent" }}
  type="primary"
  icon={<img src={icon} />}
  onClick={addToBasket}
  onMouseEnter={() => setIcon(minusCart /**for example */)}
  onMouseLeave={() => setIcon(plusIcon /**for example */)}
/>;

Upvotes: 1

Related Questions