Skip to content

Instantly share code, notes, and snippets.

@DoviMaj
Created March 18, 2019 23:17
Show Gist options
  • Save DoviMaj/ac0077fe99a8d63b0286f58f6c09c21d to your computer and use it in GitHub Desktop.
Save DoviMaj/ac0077fe99a8d63b0286f58f6c09c21d to your computer and use it in GitHub Desktop.
import React, {Component} from "react"
import "./style.css"
export default class MemeGenarator extends Component {
constructor(){
super()
this.state = {
data: [],
topText: "",
bottomText: "",
randomImage: "https://i.imgflip.com/26am.jpg"
}
this.handleSubmit = this.handleSubmit.bind(this)
this.handelChange = this.handelChange.bind(this)
}
componentDidMount(){
fetch("https://api.imgflip.com/get_memes")
.then((resp) => resp.json())
.then((resp) => {
const {memes} = resp.data
this.setState({
data: memes
})
})
}
handleSubmit(e){
e.preventDefault()
const randomNum = [Math.floor(Math.random() * (100 - 1 + 1)) + this.state.data.length]
const randomMemeImg = this.state.data[randomNum].url
this.setState({randomImage: randomMemeImg})
}
handelChange(evt){
const {name, value} = evt.target
this.setState({
[name]: value
})
}
render(){
return(
<div>
<form onSubmit={this.handleSubmit}>
<input
name="topText"
value={this.state.topText}
onChange={this.handelChange}
type="text"
placeholder="Top Text"
/>
<input
name="bottomText"
value={this.state.bottomText}
onChange={this.handelChange}
type="text"
placeholder="Bottom Text"
/>
<button>New Meme</button>
</form>
<h1 className="topText">{this.state.topText}</h1>
<img alt="Meme" src={this.state.randomImage}/>
<h1 className="bottomText">{this.state.bottomText}</h1>
</div>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment