import React, {Component} from 'react';
export class SimpleButton extends Component{
constructor(props){
super(props);
this.state = {
counter: 0,
hasButtonBeenClicked: false
}
}
render(){
return (
<button onClick={this.handleClick} className={this.props.className}
disabled={this.props.disabled==="true" || this.props.disabled === true} >
{this.props.text} {this.state.counter} {this.state.hasButtonBeenClicked && <div>Button Clicked!</div>}
</button>
)
}
handleClick = () => {
this.setState({
counter: this.state.counter + 1,
hasButtonBeenClicked: true
});
this.props.callback();
}
}
import React from 'react';
import {SimpleButton} from './SimpleButton';
export function Summary(props){
return (
<React.Fragment>
<td> {props.index + 1} </td>
<td> { props.name } </td>
<td> {props.name.length } </td>
<td>
<SimpleButton className="btn btn-warning btn-sm m-1" callback={ props.reverseCallback } text={`Reverse (${props.name})`} />
<SimpleButton className="btn btn-info btn-sm m-1" callback={ () => props.promoteCallback(props.name) } text={`Promote (${props.name})`} />
</td>
</React.Fragment>
);
}
import logo from './logo.svg';
import './App.css';
import React from 'react';
import { Summary } from './Summary';
import ReactDOM from "react-dom";
let names=["Bob", "Alice", "Dora"];
function reverseNames(){
names.reverse();
ReactDOM.render(<App />, document.getElementById('root'));
}
function promoteName(name){
names = [name, ...names.filter(val => val !== name)];
ReactDOM.render(<App />, document.getElementById('root'));
}
function App() {
return ( <table className="table table-sm table-striped">
<thead>
<tr><th>#</th><th>Name</th><th>Letters</th></tr>
</thead>
<tbody>
{
names.map( (name,index) =>
<tr key={name}><Summary index={index} name={name}
reverseCallback={reverseNames}
promoteCallback={promoteName} /> </tr>)
}
</tbody>
</table>
);
}
export default App;