Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The old class way
- class OldWay extends React.Component {
- constructor(props) {
- super(props);
- this.state = { count: 0 };
- }
- render() {
- return (
- <div>
- <div>You have clicked {this.state.count} times.</div>
- <button onClick={() => this.setState({ count: this.state.count + 1 })}>Click</button>
- </div>
- );
- }
- }
- // Hooks let you do this!
- import React, { useState } from 'react';
- function NewWay() {
- // Declare new state variable called "count"
- const [count, setCount] = useState(0);
- return (
- <div>
- <div>You have clicked {this.state.count} times.</div>
- <button onClick={() => this.setState({ count: this.state.count + 1 })}>Click</button>
- </div>
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement