Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export default class Child extends Component {
- render(){
- return(
- <button
- onClick={this.props.onClick}
- className={this.props.fadeAway ? 'active' : ''}>
- {this.props.text}
- </button>
- )
- }
- }
- export default class Parent extends Component {
- constructor(props) {
- super(props);
- this.state = {
- selectedIndex: null,
- };
- }
- handleClick (selectedIndex) {
- this.setState({
- selectedIndex,
- });
- }
- render () {
- const array = ["Button 1","Button 2"];
- return (
- <div>
- {array.map((obj, index) => {
- const faded = this.state.selectedIndex === index;
- return <Child
- text={obj}
- fadeAway={faded}
- key={index}
- onClick={() => this.handleClick(index)} />
- })}
- </div>
- )
- }
- }
Add Comment
Please, Sign In to add comment