Advertisement
irobust

React demo 02

Jul 10th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class PlusButton extends React.Component{
  2.     handleOnClick = () => {
  3.         this.props.action(this.props.step);    
  4.   }
  5.     render(){
  6.     return (
  7.         <button onClick={this.handleOnClick}>+ {this.props.step}</button>
  8.     );
  9.   }
  10. }
  11.  
  12. class MinusButton extends React.Component{
  13.     render(){
  14.     return (
  15.         <button onClick={this.props.action}>-</button>
  16.     );
  17.   }
  18. }
  19.  
  20. class App extends React.Component{
  21.     state = {counter: 0};
  22.  
  23.   increase = (step) => {
  24.     this.setState((prevState) => {
  25.         return {
  26.         counter: prevState.counter + step
  27.       }
  28.     })
  29.   }
  30.  
  31.   decrease = () => {
  32.     this.setState((prevState) => {
  33.         return {
  34.         counter: prevState.counter - 1
  35.       }
  36.     })
  37.   }
  38.  
  39.     render(){
  40.     return (
  41.       <div>
  42.         <PlusButton step={5}
  43.                 action={this.increase} />
  44.         <PlusButton step={1}
  45.                 action={this.increase} />
  46.         <MinusButton action={this.decrease} />
  47.         <div>{this.state.counter}</div>
  48.       </div>
  49.     );
  50.   }
  51. }
  52.  
  53. ReactDOM.render(<App />, mountNode);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement