Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2.  
  3. class ButtonToClick extends React.Component {
  4.     handleClick = () => {
  5.         if (typeof this.props.fn === 'function') {
  6.             this.props.fn();
  7.         }
  8.     }
  9.  
  10.     render() {
  11.         return <button onClick={this.handleClick}>Klik</button>
  12.     }
  13. }
  14.  
  15. export default class ButtonCounter extends React.Component {
  16.     constructor(props) {
  17.         super(props);
  18.  
  19.         this.state = {
  20.             counter: 0
  21.         }
  22.     }
  23.  
  24.     increment = () => {
  25.         this.setState({
  26.             counter: this.state.counter + 1
  27.         });
  28.     }
  29.  
  30.     render() {
  31.         return (
  32.             <div>
  33.                 <h1>{this.state.counter}</h1>
  34.                 <ButtonToClick fn={this.increment}/>
  35.                 <ButtonToClick fn={this.increment}/>
  36.             </div>
  37.         )
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement