scrimba
Learn React for free
useEffect() Part 2
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

useEffect() Part 2
AboutCommentsNotes
useEffect() Part 2
Expand for more info
App.js
run
preview
console
import React, {useState, useEffect} from "react"
import randomcolor from "randomcolor"

function App() {
const [count, setCount] = useState(0)
const [color, setColor] = useState("")

function increment() {
setCount(prevCount => prevCount + 1)
}

function decrement() {
setCount(prevCount => prevCount - 1)
}

useEffect(() => {
setColor(randomcolor())
}, [count])

return (
<div>
<h1 style={{color: color}}>{count}</h1>
<button onClick={increment}>Increment</button>
<button onClick={decrement}>Decrement</button>
</div>
)
}

export default App
Console
/index.html
-10:16