REACT - toggle class onclick

后端 未结 14 1922
长情又很酷
长情又很酷 2020-11-27 11:40

I am trying to figure out how to toggle an active class onClick to change CSS properties.

I have taken many approaches, and read many SO answers. Using

14条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 12:05

    You can also do this with hooks.

    function MyComponent (props) {
      const [isActive, setActive] = useState(false);
    
      const toggleClass = () => {
        setActive(!isActive);
      };
    
      return (
        

    {props.text}

    ); }

提交回复
热议问题