Last active
May 7, 2019 14:37
-
-
Save tmli3b3rm4n/e6e102cf3cf156f880c1e1c014aaf1ea to your computer and use it in GitHub Desktop.
how to component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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