Do I need for each list item a boolean in the state object?
Yes, you need to maintain a boolean for each list item in state variable, and on the basis of that state value, render the content of list items, Try this:
this.state = {showItems: []}
onClick(index){
console.log(index);
let showItems = this.state.showItems.slice();
showItems[index] = !showItems[index];
this.setState({showItems});
}
<ul>
<li onClick={this.onClick.bind(this,0)}> item {this.state.showItems[0] ? <div> some extra text ... </div> : null} </li>
<li onClick={this.onClick.bind(this,1)}> item {this.state.showItems[1] ? <div> some extra text ... </div> : null}</li>
<li onClick={this.onClick.bind(this,2)}> item{this.state.showItems[2] ? <div> some extra text ... </div> : null} </li>
<li onClick={this.onClick.bind(this,3)}> item {this.state.showItems[3] ? <div> some extra text ... </div> : null}</li>
</ul>
Check this working Snippet:
class App extends React.Component {
constructor(){
super();
this.state = {showItems:[]}
}
onClick(index){
let showItems = this.state.showItems.slice(0);
showItems[index] = !showItems[index];
this.setState({showItems});
}
render() {
return (
<div className="App">
<ul>
<li onClick={this.onClick.bind(this,0)}> item {this.state.showItems[0] ? <div> some extra text ... </div> : null} </li>
<li onClick={this.onClick.bind(this,1)}> item {this.state.showItems[1] ? <div> some extra text ... </div> : null}</li>
<li onClick={this.onClick.bind(this,2)}> item{this.state.showItems[2] ? <div> some extra text ... </div> : null} </li>
<li onClick={this.onClick.bind(this,3)}> item {this.state.showItems[3] ? <div> some extra text ... </div> : null}</li>
</ul>
<div style={{marginTop: 100}}>*click on item to open submenu</div>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('container'));
li{
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='container'/>
Check fiddle for working example: https://jsfiddle.net/mayankshukla5031/18hheyx1/