Toggle Class in React

后端 未结 5 912
误落风尘
误落风尘 2020-12-13 18:27

I\'m using react for a project where I have a menu button.

         


        
5条回答
  •  粉色の甜心
    2020-12-13 18:59

    Toggle function in react

    At first you should create constructor like this

    constructor(props) {
            super(props);
            this.state = {
                close: true,
            };
        }
    

    Then create a function like this

    yourFunction = () => {
            this.setState({
                close: !this.state.close,
            });
        };
    

    then use this like

    render() {
            const {close} = this.state;
            return (
    
                
    
                     
    this.yourFunction()}>
    ) } }

    Please give better solutions

提交回复
热议问题