Created
September 28, 2020 13:15
-
-
Save dearshrewdwit/6d774bb51e701858edca364a66e5be8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using class component to manage state | |
import React, { Component } from 'react' | |
class Counter extends Component { | |
constructor(props) { | |
super(props) | |
this.state = { counter: 0 } | |
} | |
addOne = () => { | |
this.setState({counter: this.state.counter + 1}) | |
} | |
render() { | |
return ( | |
<div> | |
{this.state.counter} | |
<button onClick={this.addOne}>add</button> | |
</div> | |
) | |
} | |
} | |
export default Counter | |
// using functional component with state hook to manage state | |
import React, { useState, useEffect } from 'react' | |
const Counter = () => { | |
const [counter, setCounter] = useState(0) | |
const addOne = () => setCounter(counter + 1) | |
return ( | |
<div> | |
<p>{counter}</p> | |
<button onClick={addOne}>add</button> | |
</div> | |
) | |
} | |
export default Counter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment