Explorer
project
components
Header.js
Meme.js
images
troll-face.png
App.js
index.html
index.js
memesData.js
style.css
Dependencies
react-dom@17.0.2
react@17.0.2
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
import React from "react"
import memesData from "../memesData.js"
export default function Meme() {
/**
* Challenge:
* As soon as the Meme component loads the first time,
* make an API call to "https://api.imgflip.com/get_memes".
*
* When the data comes in, save just the memes array part
* of that data to the `allMemes` state
*
* Think about if there are any dependencies that, if they
* changed, you'd want to cause to re-run this function.
*
* Hint: for now, don't try to use an async/await function.
* Instead, use `.then()` blocks to resolve the promises
* from using `fetch`. We'll learn why after this challenge.
*/
const [meme, setMeme] = React.useState({
topText: "",
bottomText: "",
randomImage: "http://i.imgflip.com/1bij.jpg"
})
const [allMemes, setAllMemes] = React.useState(memesData)
function getMemeImage() {
const memesArray = allMemes.data.memes
const randomNumber = Math.floor(Math.random() * memesArray.length)
const url = memesArray[randomNumber].url
setMeme(prevMeme => ({
...prevMeme,
randomImage: url
}))
}
function handleChange(event) {
const {name, value} = event.target
setMeme(prevMeme => ({
...prevMeme,
[name]: value
}))
}
return (
<main>
<div className="form">
<input
type="text"
placeholder="Top text"
className="form--input"
name="topText"
value={meme.topText}
onChange={handleChange}
/>
<input
type="text"
placeholder="Bottom text"
className="form--input"
name="bottomText"
value={meme.bottomText}
onChange={handleChange}
/>
<button
className="form--button"
onClick={getMemeImage}
>
Get a new meme image 🖼
</button>
</div>
<div className="meme">
<img src={meme.randomImage} className="meme--image" />
<h2 className="meme--text top">{meme.topText}</h2>
<h2 className="meme--text bottom">{meme.bottomText}</h2>
</div>
</main>
)
}