scrimba
Frontend Career Path
React basics
Meme generator
useState - Changing state with a callback function
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

useState - Changing state with a callback function
AboutCommentsNotes
useState - Changing state with a callback function
Expand for more info
App.js
run
preview
console
import React from "react"

export default function App() {
const [count, setCount] = React.useState(0)

function add() {
setCount(count + 1)
}

function subtract() {
setCount(count - 1)
}

return (
<div className="counter">
<button className="counter--minus" onClick={subtract}>–</button>
<div className="counter--count">
<h1>{count}</h1>
</div>
<button className="counter--plus" onClick={add}>+</button>
</div>
)
}
Console
/index.html
-4:18