Guest User

Dziala

a guest
Apr 30th, 2019
154
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Show extends React.Component {
  2.   render() {
  3.     return (
  4.       <div>
  5.         <output>{this.props.number}</output>
  6.         <div>
  7.         <button name="plus" onClick={this.props.handlePlus}>+</button>
  8.         <button name="minus" onClick={this.props.handleMinus}>-</button>
  9.         </div>
  10.       </div>
  11.     );
  12.   };
  13. }
  14.  
  15. class App extends React.Component {
  16.   constructor(props) {
  17.     super(props);
  18.     this.state = { number: 0 };
  19.   };
  20.  
  21.   handleChange = (e) => {
  22.     const value = parseFloat(e.target.value);
  23.     if(Number.isNaN(value)){
  24.       return '';
  25.     }
  26.     this.setState({ number: value });
  27.   };
  28.  
  29.   handlePlus = () => {
  30.     this.setState({ number: this.state.number + 1 })
  31.   }
  32.   handleMinus = () => {
  33.     this.setState({ number: this.state.number - 1 })
  34.   }
  35.  
  36.   render() {
  37.     return (
  38.       <div>
  39.         <title>Podaj liczbe:</title>
  40.         <input value={this.state.value} onChange={this.handleChange}></input>
  41.         <Show
  42.           number={this.state.number}
  43.           handlePlus={this.handlePlus}
  44.           handleMinus={this.handleMinus}
  45.         />
  46.       </div>
  47.     );
  48.   };
  49. }
  50.  
  51. ReactDOM.render(
  52.   <App />,
  53.   document.getElementById('root')
  54. );
RAW Paste Data