Skip to content

Instantly share code, notes, and snippets.

@SaraVieira
Last active July 6, 2018 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SaraVieira/d31720cb3185bc0f64d5ceba7466c73c to your computer and use it in GitHub Desktop.
Save SaraVieira/d31720cb3185bc0f64d5ceba7466c73c to your computer and use it in GitHub Desktop.
class Wrapper extends React.Component {
constructor(props) {
super(props)
this.state = { image: null }
this.getImage = this.getImage.bind(this)
}
getImage() {
return fetch('https://dog.ceo/api/breed/akita/images/random') // Get The image
.then(rsp => rsp.json()) // Transform the response to JSON
.then(data =>
this.setState({
image: data.message // Set the state.image equal to response of the api
})
)
}
componentDidMount() {
// https://reactjs.org/docs/react-component.html#componentdidmount
this.getImage()
}
render() {
return React.createElement(
'div',
{ className: 'wrapper' },
React.createElement(Img, { source: this.state.image }),
React.createElement(Button, { getImage: this.getImage })
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment