Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3.  
  4. class ButtonCounter extends React.Component {
  5. state = {
  6. counter: 0,
  7. }
  8.  
  9. handleBtnClick = () => {
  10. this.setState({
  11. counter: this.state.counter +1,
  12. });
  13. }
  14.  
  15. render() {
  16. return (
  17. <div>
  18. <h1>{this.state.counter}</h1>
  19. <ButtonToClick onClick={this.handleBtnClick} />
  20. <ButtonToClick onClick={this.handleBtnClick} />
  21. </div>
  22. )
  23. }
  24. }
  25.  
  26.  
  27. class ButtonToClick extends React.Component {
  28. handleClick = () => {
  29. if(typeof this.props.onClick === 'function') {
  30. this.props.onClick();
  31. }
  32. }
  33.  
  34. render() {
  35. return (
  36. <button onClick={this.handleClick}>click me</button>
  37. )
  38. }
  39. }
  40.  
  41.  
  42. class App extends React.Component {
  43.  
  44. render() {
  45. return <ButtonCounter />
  46. }
  47. }
  48.  
  49.  
  50.  
  51. document.addEventListener('DOMContentLoaded', function () {
  52. ReactDOM.render(
  53. <App />,
  54. document.getElementById('app')
  55. );
  56. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement