Created
August 9, 2018 08:26
-
-
Save ristaa/04e871e739bb5ee4024dbac8ee4efce4 to your computer and use it in GitHub Desktop.
React simple fetch method with JSON
This file contains 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, { Component } from "react"; | |
import { render } from "react-dom"; | |
class App extends Component { | |
state = { | |
data: [] | |
}; | |
componentDidMount() { | |
this.getDataFromApi(); | |
} | |
getDataFromApi = () => { | |
fetch("https://facebook.github.io/react-native/movies.json") | |
.then(response => response.json()) | |
.then(data => { | |
this.setState({ data: data.movies }); | |
}) | |
.catch(error => { | |
this.setState({ error: error }); | |
}); | |
}; | |
render() { | |
const list = this.state.data.map((item, index) => ( | |
<div key={index}>{item.title} </div> | |
)); | |
return <div className="App">{list}</div>; | |
} | |
} | |
render(<App />, document.getElementById("root")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment