简体   繁体   中英

How to react function component in like a toggle button

I want the icon components to change after pressing the Like button, but it doesn't go my way. Would it be possible to get some help?

"Heart" and "HeartOutline" components call icons.

const [heart, setHeartRed] = useState({
    state: false,
    number: '0',
 });

const isLikeyOnHandler = () => {
    heart.state
      ? setHeartRed({ state: false, number: '0' })
      : setHeartRed({ state: true, number: '1' });
  };

return(
    <HeartButton
      text={heart.number}
      icon={heart.state ? <Heart /> : <HeartOutline />}
      onClick={() => isLikeyOnHandler()}
    />
)

const [heart, setHeartRed] = useState({
    state: false,
    number: '0',
 });

const isLikeyOnHandler = () => {
setHeartRed((prevState)=>({state:!prevState.state,number:+!+prevState.number+""})
  };

return(
    <HeartButton
      text={heart.number}
      icon={heart.state ? <Heart /> : <HeartOutline />}
      onClick={isLikeyOnHandler}
    />
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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