Skip to content

Instantly share code, notes, and snippets.

@tmli3b3rm4n
Last active May 7, 2019 14:37
Show Gist options
  • Save tmli3b3rm4n/e6e102cf3cf156f880c1e1c014aaf1ea to your computer and use it in GitHub Desktop.
Save tmli3b3rm4n/e6e102cf3cf156f880c1e1c014aaf1ea to your computer and use it in GitHub Desktop.
how to component
/// How to access multiple
class Menu extends React.Component {
constructor(){
super()
this.state = {
items: ['Hello', 'world', 'it', 'is', 'me'],
active: 0
}
}
click(index){
this.setState({active: index})
}
render(){
const list = this.state.items.map((item,index) => {
return <Item key={index}
className={this.state.active === index ? 'active' : ''}
text={item}
click={this.click.bind(this, index)}
/>
})
return <ul>
{list}
</ul>
}
}
class Item extends React.Component {
render(){
return <li onClick={this.props.click}
className={this.props.className}>
{this.props.text}
</li>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment