I am trying to toggle the visibility of an menu in a React app. I have tried by setting the state property that I change on click. And then I am checking the objects state to toggle visibility. This is the code:
constructor(props) {
super(props);
this.state = {
'menuOpen': false,
}
}
openMenu() {
var newState = !this.state.menuOpen;
this.setState({
'menuOpen': newState
});
}
var menuList = React.createClass({
render: function() {
return (
<div className={styles.menuList}>
<ul>
<li>Link</li>
<li>Link</li>
<li>Link</li>
</ul>
</div>
);
}
});
render() {
return (
<div className={styles.menu}><span className={styles.menuIcon} onClick={this.openMenu.bind(this)} />
{this.state.menuOpen ? <menuList /> : null}
</div>
);
}
I get an error:
> in ./src/components/post/index.js Module build failed: SyntaxError:
> Unexpected token (31:8)
>
> }
> var menuList = React.createClass({
> ^
What am I doing wrong?
vardeclarations. Try moving yourmenuListcomponent into its own class declaration.