Skip to content

Instantly share code, notes, and snippets.

@dearshrewdwit
Created September 28, 2020 13:15
Show Gist options
  • Save dearshrewdwit/6d774bb51e701858edca364a66e5be8c to your computer and use it in GitHub Desktop.
Save dearshrewdwit/6d774bb51e701858edca364a66e5be8c to your computer and use it in GitHub Desktop.
// 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