Skip to content

Instantly share code, notes, and snippets.

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