scrimba
Frontend Career Path
React basics
Meme generator
Project: Add text to image
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: Add text to image
Expand for more info
components
Meme.js
run
preview
console
import React from "react"
import memesData from "../memesData.js"

export default function Meme() {
/**
* Challenge:
* 1. Set up the text inputs to save to
* the `topText` and `bottomText` state variables.
* 2. Replace the hard-coded text on the image with
* the text being saved to state.
*/

const [meme, setMeme] = React.useState({
topText: "",
bottomText: "",
randomImage: "http://i.imgflip.com/1bij.jpg"
})
const [allMemeImages, setAllMemeImages] = React.useState(memesData)


function getMemeImage() {
const memesArray = allMemeImages.data.memes
const randomNumber = Math.floor(Math.random() * memesArray.length)
const url = memesArray[randomNumber].url
setMeme(prevMeme => ({
...prevMeme,
randomImage: 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>
<div className="meme">
<img src={meme.randomImage} className="meme--image" />
<h2 className="meme--text top">One does not simply</h2>
<h2 className="meme--text bottom">Walk into Mordor</h2>
</div>
</main>
)
}
Console
/index.html
-3:58