Skip to content

Instantly share code, notes, and snippets.

@santosh
Created March 16, 2020 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santosh/acb2cd9554df66f3670f518c13c4e640 to your computer and use it in GitHub Desktop.
Save santosh/acb2cd9554df66f3670f518c13c4e640 to your computer and use it in GitHub Desktop.
Using states in functional components.
function App() {
const [count, setCount] = React.useState(0)
function increment() {
setCount(prevCount => prevCount + 1)
}
function decrement() {
setCount(prevCount => prevCount - 1)
}
return (
<div>
<h1>{count}</h1>
<button onClick={increment}>Increment!</button>
<button onClick={decrement}>Decrement!</button>
</div>
)
}
ReactDOM.render(<App />, document.getElementById("root"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment