Skip to content

Instantly share code, notes, and snippets.

@anghelalexandra
Created October 19, 2017 08:14
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 anghelalexandra/317542a1ccfa6a754d6b18bb99d78571 to your computer and use it in GitHub Desktop.
Save anghelalexandra/317542a1ccfa6a754d6b18bb99d78571 to your computer and use it in GitHub Desktop.
Reading a list of categories from the API in a React component
import React, { Component } from "react";
import config from "./config";
import CategoriesList from "./CategoriesList";
class Categories extends Component {
constructor(props) {
super(props);
this.state = {
categories: []
};
}
componentWillMount() {
fetch(config.API_CATEGORIES_URL)
.then(response => response.json())
.then(json => this.setState({ categories: json }))
.catch(() => {
console.log("An error occured");
});
}
render() {
return <CategoriesList categories={this.state.categories} />;
}
}
export default Categories;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment