Skip to content

Instantly share code, notes, and snippets.

@SaraVieira SaraVieira/block9.js Secret
Last active Jul 6, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.