Guest User

Untitled

a guest
Oct 15th, 2019
83
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // The old class way
  2.  
  3. class OldWay extends React.Component {
  4. constructor(props) {
  5. super(props);
  6. this.state = { count: 0 };
  7. }
  8.  
  9. render() {
  10. return (
  11. <div>
  12. <div>You have clicked {this.state.count} times.</div>
  13. <button onClick={() => this.setState({ count: this.state.count + 1 })}>Click</button>
  14. </div>
  15. );
  16. }
  17. }
  18.  
  19. // Hooks let you do this!
  20.  
  21. import React, { useState } from 'react';
  22.  
  23. function NewWay() {
  24. // Declare new state variable called "count"
  25. const [count, setCount] = useState(0);
  26.  
  27. return (
  28. <div>
  29. <div>You have clicked {this.state.count} times.</div>
  30. <button onClick={() => this.setState({ count: this.state.count + 1 })}>Click</button>
  31. </div>
  32. );
  33. }
RAW Paste Data