Advertisement
Guest User

Untitled

a guest
May 26th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. // ES6, React 0.14
  2. var MenuItem = ({onClick, text, active}) => (
  3. <button onClick={onClick} style={active? {color: 'red'} : null}>{text}</button>
  4. );
  5.  
  6. // example of use: <MenuBar buttons={['cat', 'dog', 'penguin']}/>
  7. class MenuBar extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {activeIndex: 0};
  11. }
  12. handleItemClick(index) {
  13. this.setState({activeIndex: index});
  14. }
  15. render() {
  16. var activeIndex = this.state.activeIndex;
  17. return <div>
  18. {
  19. this.props.buttons.map(
  20. (btn, i) => (
  21. <MenuItem
  22. active={activeIndex === i}
  23. key={i}
  24. onClick={this.handleItemClick.bind(this, i)}
  25. text={btn} />
  26. )
  27. )
  28. }
  29. </div>
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement