Created
October 14, 2017 17:44
-
-
Save hpneo/d5067103dd103e370dfaf29fd66df390 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react'; | |
| export default class FetchData extends React.Component { | |
| constructor() { | |
| super(); | |
| this.state = { | |
| data: null, | |
| isLoading: false, | |
| error: '', | |
| }; | |
| } | |
| componentDidMount() { | |
| this.setState({ isLoading: true }); | |
| fetch('https://httpbin.org/anything') | |
| .then(response => response.json()) | |
| .then((data) => this.setState({ | |
| isLoading: false, | |
| data: data, | |
| })) | |
| .catch(error => { | |
| console.log(error); | |
| this.setState({ | |
| isLoading: false, | |
| error: error.toString() | |
| }); | |
| }); | |
| } | |
| render() { | |
| if (this.state.isLoading) { | |
| return <div>Cargando...</div>; | |
| } | |
| if (this.state.error) { | |
| return <div>{this.state.error}</div>; | |
| } | |
| console.log(this.state.data); | |
| return <div>{JSON.stringify(this.state.data)}</div> | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment