showing results for - "toggle css class in react"
April
15 Mar 2019
1//toggle css class in react using mouse clicks
2//solution
3class foo extends Component {
4  	constructor(props){
5        super(props);
6        this.state={
7            isToggle:false
8        }    
9    }
10  render() {
11            return (
12                <div className={this.state.isToggle ? "myClass":null} 
13  				onClick={()=>{this.setState({isToggle:!this.state.isToggle});}}>           
14                        <div className="box">    
15                        </div>    
16                </div>
17            );   
18    }
19}
20//or in more elegant way
21
22class foo extends Component {
23  	constructor(props){
24        super(props);
25 ////////////////////////////////////////
26      // this variable will be used to know when should your progam use the class or not
27        this.state={
28            isToggle:false
29        }    
30////////////////////////////////////////
31    }
32  render() {
33            return (
34////////////////////////////////////////     
35              // if (this.state.isToggle) then "myClass" else null
36                <div className={this.state.isToggle ? "myClass":null} 
37////////////////////////////////////////
38////////////////////////////////////////   
39  			// when the current div is clicked negate isToggle
40  				onClick={()=>{this.setState({isToggle:!this.state.isToggle});}}>           
41                        <div className="box">    
42                        </div>    
43                </div>
44////////////////////////////////////////   
45            );   
46    }
47}
queries leading to this page
how to toggle html class in reactreact toggle css namereact toggle classnametoggle css reacttoggle classes reacttoggle css class in reacttoggle classname reacthow to toggle classname in react jstoggle cssclass on reacttoggle a class in reacttoggleclass reacttoggle class on reacttoggle styling with react jsreact toggle css classtoggleclass in reacthow to toggle a class in react js toggle class reacttoggle styles reacttoggle css classname reacthow to toggle classname in reacttoggle classname react jstoggle classnames in react on clickclassnames react toggleclassname toggle in reacttoggle class in reacthow to toggle class reactreact toggle classreact toggle component classname attributehow to toggle css class in reacttoggle classname react functionreact component toggle classnameclassname toggle reactreact toggle component classnamereact toggle css classnamehow to toggle class through reacthow to toggle classname on reactreact toggle classnamestoggle react classnamereact toogle classnametoggle class reacthow to toggle css in react jstoggle a class with reacttoggle class using reacthow to toggle class in reactcustom css toggle reactjsreact class componet toggle stae toggleclass reacthow to toggle class in react jsclassnames toggle reacttoggle react classhow to toggle a style property in reactreactjs toggle classeshow to toggle a class in reacttoggle class using reactjsreact toggle 2fstyle cssclass toggle reacthow to toggle between style reacttoggle css class reacttoggle style reacttoggle react component classnametoggle class react jstoggle style element reacthow to toggle class using reacttoggle classname in reactreact toggle css class on clickreactjs toggle classtoggle calss in reacttoggle classname react function componenttoggle classname using function method react jstoggle class name reacttoggle class to component reacttoggle css class in react