Videos
Where can I find the Syncfusion React Toggle Switch Button demo?
Why should you choose Syncfusion React Toggle Switch Button?
How do I get started with Syncfusion React Toggle Switch Button?
ยป npm install react-toggle-button
The initial value of state is false. So, after you do the first click, you set state to !state, which makes it true (because !false equals true). On the second click, it will become false because state was true and setting it to !state made it false (!true equals false). The exclamation mark (!) is read as not.
By the way, I recommend setting the state like this:
setState(prev => !prev);
Hope this helps.
In react when you setState and want to use the previous value as the input you should do this:
setState(pre => !pre)
Here we get the current value by using pre in the useState function. This should work perfectly.
By moving the State into the main ToggleMenu, you can have this component maintain the visibility of the Menu.
class ToggleMenu extends React.Component {
constructor(props) {
super(props);
this.state = {
visible: false
};
this.toggleMenu = this.toggleMenu.bind(this);
}
toggleMenu() {
this.setState({visible: !this.state.visible})
}
render() {
return (
<div>
<button onClick={this.toggleMenu}>Show Right Menu!</button>
{this.state.visible && <Menu alignment="right">
<MenuItem hash="first-page">First Page</MenuItem>
<MenuItem hash="second-page">Second Page</MenuItem>
<MenuItem hash="third-page">Third Page</MenuItem>
</Menu>}
</div>
);
}
}
This allowed me to change your menu into a Stateless Component:
const Menu = ({alignment, children}) => (
<div className="menu">
<div className={alignment}>{children}</div>
</div>
);
I have created a webpackbin here (now with animation): https://www.webpackbin.com/bins/-Klh1VM-n4RDCkEbkK67
For transitions and animation, I recommend you look at https://github.com/reactjs/react-transition-group
export default class ToggleMenu extends React.Component {
constructor(props) {
super(props);
this.state = { rightMenu: false }
this.showRight = this.showRight.bind(this);
}
showRight = () => {
this.setState({ rightMenu: !this.state.rightMenu })
}
render() {
return (
<div>
<button onClick={this.showRight}>Show Right Menu!</button>
<Menu className={ this.state.rightMenu ? "displayBlock" : "displayNone"} ref={right => this.right = right} alignment="right">
<MenuItem hash="first-page">First Page</MenuItem>
<MenuItem hash="second-page">Second Page</MenuItem>
<MenuItem hash="third-page">Third Page</MenuItem>
</Menu>
</div>
);
}
}
//Style code
.displayBlock{ display: block }
.displayNone{ display: none}