Last active
May 2, 2019 04:46
-
-
Save jlittlejohn/e913c2ba3eab883bd4d21fccd200d07b to your computer and use it in GitHub Desktop.
REACT: API Call Example
This file contains hidden or 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
constructor(props) { | |
super(props); | |
this.state = { | |
items: [] | |
}; | |
} | |
componentDidMount() { | |
fetch("http://example.com/api/endpoint/") | |
//fetch("https://cors.io/?http://example.com/api/endpoint/") | |
.then(response => response.json()) | |
.then(responseData => { | |
this.setState({ | |
items: responseData.results | |
}); | |
}) | |
.catch(error => { | |
console.log("Fetching and parsing data error", error); | |
}); | |
} | |
render() { | |
let items = this.state.items; | |
console.log(items); | |
return ( | |
<div> | |
{items.map(function(item) { | |
return ( | |
<h4 key={item.id}> | |
{item.name} | |
</h4> | |
); | |
})} | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment