import React from 'react';
/**
App
Simple react js fetch example */ class App extends React.Component {
constructor(props) {
super(props); this.state = { items: [], isLoaded: false }
}
/**
componentDidMount
Fetch json array of objects from given url and update state. */ componentDidMount() {
fetch('https://run.mocky.io/v3/8260aa5d-8af8-4cff-999e-6e81b217f0ba') .then(res => res.json()) .then(json => { this.setState({ items: json, isLoaded: true, }) }).catch((err) => { console.log(err); });
}
/**
render
Render UI */ render() {
const { isLoaded, items } = this.state;
if (!isLoaded) return Loading...;
return (
-
{items.map(item => (
Name: {item.name}
))}
}
}
export default App;