I'm trying to access CSS class with react.
Here is my CSS file:
body {
overflow-x: hidden;
}
#sidebar-wrapper {
min-height: 100vh;
margin-left: -15rem;
-webkit-transition: margin .25s ease-out;
-moz-transition: margin .25s ease-out;
-o-transition: margin .25s ease-out;
transition: margin .25s ease-out;
}
#sidebar-wrapper .sidebar-heading {
padding: 0.875rem 1.25rem;
font-size: 1.2rem;
}
#sidebar-wrapper .list-group {
width: 15rem;
}
#page-content-wrapper {
min-width: 100vw;
}
#wrapper.toggled #sidebar-wrapper {
margin-left: 0;
}
@media (min-width: 768px) {
#sidebar-wrapper {
margin-left: 0;
}
#page-content-wrapper {
min-width: 0;
width: 100%;
}
#wrapper.toggled #sidebar-wrapper {
margin-left: -15rem;
}
}
in jQuery it is this:
$("#menu-toggle").click(function(e) {
e.preventDefault();
$("#wrapper").toggleClass("toggled");
});
I found in React people do like this:
import React from 'react';
import './sidebar.component.css';
class SidebarWrapper extends React.Component {
constructor(props) {
super(props);
this.state = {
toggled: true,
bgColor: 'black'
};
this.toggleMenu = this.toggleMenu.bind(this);
}
toggleMenu= (e) => {
this.setState({toggled: !this.state.toggled})
this.setState({bgColor: 'red'})
console.log(this.state.toggled)
console.log(this.state.bgColor)
}
render() {
let buttonClass = (this.state.toggled) ? 'true' : 'false';
return (
<div className={buttonClass}>
<button id="menu-toggle" style={{backgroundColor: this.state.bgColor }} onClick={this.toggleMenu}>Toggle Menu</button>
</div>
);
}
};
export default SidebarWrapper;
But nothing happens except for the color change. I'm thinking that the wrapper.toggle css class needs to be accessed somehow via react, but how?
I cannot do this.state {wrapper.toggle = true}
Is there something I am doing wrong?