scrimba
Frontend Career Path
React basics
Meme generator
Project: Refactor state
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

AboutCommentsNotes
Project: Refactor state
Expand for more info
components
Meme.js
run
preview
console
import React from "react"
import memesData from "../memesData.js"

export default function Meme() {
/**
* Challenge: Update our state to save the meme-related
* data as an object called `meme`. It should have the
* following 3 properties:
* topText, bottomText, randomImage.
*
* The 2 text states can default to empty strings for now,
* amd randomImage should default to "http://i.imgflip.com/1bij.jpg"
*
* Next, create a new state variable called `allMemeImages`
* which will default to `memesData`, which we imported above
*
* Lastly, update the `getMemeImage` function and the markup
* to reflect our newly reformed state object and array in the
* correct way.
*/

const [memeImage, setMemeImage] = React.useState("http://i.imgflip.com/1bij.jpg")


function getMemeImage() {
const memesArray = memesData.data.memes
const randomNumber = Math.floor(Math.random() * memesArray.length)
setMemeImage(memesArray[randomNumber].url)

}

return (
<main>
<div className="form">
<input
type="text"
placeholder="Top text"
className="form--input"
/>
<input
type="text"
placeholder="Bottom text"
className="form--input"
/>
<button
className="form--button"
onClick={getMemeImage}
>
Get a new meme image 🖼
</button>
</div>
<img src={memeImage} className="meme--image" />
</main>
)
}
Console
/index.html
-4:10