REACT - toggle class onclick

后端 未结 14 1976
长情又很酷
长情又很酷 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:14

    Here is a code I came Up with:

    import React, {Component} from "react";
    import './header.css'
    
    export default class Header extends Component{
        state = {
            active : false
        };
    
    
        toggleMenuSwitch = () => {
            this.setState((state)=>{
                return{
                    active: !state.active
                }
            })
        };
        render() {
            //destructuring
            const {active} = this.state;
    
            let className = 'toggle__sidebar';
    
            if(active){
                className += ' active';
            }
    
            return(
                
    ); }; };

提交回复
热议问题