Skip to content

Instantly share code, notes, and snippets.

@ristaa
Created August 9, 2018 08:26
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 ristaa/04e871e739bb5ee4024dbac8ee4efce4 to your computer and use it in GitHub Desktop.
Save ristaa/04e871e739bb5ee4024dbac8ee4efce4 to your computer and use it in GitHub Desktop.
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