Guest User

Untitled

a guest
Nov 20th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. export default class Child extends Component {
  2. render(){
  3. return(
  4. <button
  5. onClick={this.props.onClick}
  6. className={this.props.fadeAway ? 'active' : ''}>
  7. {this.props.text}
  8. </button>
  9. )
  10. }
  11. }
  12.  
  13. export default class Parent extends Component {
  14. constructor(props) {
  15. super(props);
  16. this.state = {
  17. selectedIndex: null,
  18. };
  19. }
  20.  
  21. handleClick (selectedIndex) {
  22. this.setState({
  23. selectedIndex,
  24. });
  25. }
  26.  
  27. render () {
  28. const array = ["Button 1","Button 2"];
  29. return (
  30. <div>
  31. {array.map((obj, index) => {
  32. const faded = this.state.selectedIndex === index;
  33. return <Child
  34. text={obj}
  35. fadeAway={faded}
  36. key={index}
  37. onClick={() => this.handleClick(index)} />
  38. })}
  39. </div>
  40. )
  41. }
  42. }
Add Comment
Please, Sign In to add comment