scrimba
Learn React
Meme generator
useEffect for fetching data
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 for fetching data
AboutCommentsNotes
useEffect for fetching data
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(0)

/**
* Quiz:
* 1. What will happen if I put back our Star Wars API call
* into the effect function?
*/
React.useEffect(function() {
// fetch("https://swapi.dev/api/people/1")
// .then(res => res.json())
// .then(data => console.log(data))
}, [count])

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


Console
/index.html
-5:48