Created
October 19, 2017 08:14
-
-
Save anghelalexandra/317542a1ccfa6a754d6b18bb99d78571 to your computer and use it in GitHub Desktop.
Reading a list of categories from the API in a React component
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 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