scrimba
Frontend Career Path
React basics
Meme generator
useEffect: when to use dependencies
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: when to use dependencies
AboutCommentsNotes
useEffect: when to use dependencies
Expand for more info
App.js
run
preview
console
import React from "react"

export default function App() {
const [starWarsData, setStarWarsData] = React.useState({})
const [count, setCount] = React.useState(1)

/**
* Challenge: Combine `count` with the request URL
* so pressing the "Get Next Character" button will
* get a new character from the Star Wars API.
* Remember: don't forget to consider the dependencies
* array!
*/

React.useEffect(function() {
console.log("Effect ran")
fetch("https://swapi.dev/api/people/1")
.then(res => res.json())
.then(data => setStarWarsData(data))
}, [])

return (
<div>
<h2>The count is {count}</h2>
<button onClick={() => setCount(prevCount => prevCount + 1)}>Get Next Character</button>
<pre>{JSON.stringify(starWarsData, null, 2)}</pre>
</div>
)
}


Console
"Effect ran"
,
/index.html
-3:02