import React, { Component } from 'react';
export class Articles extends Component {
//--------< component: Articles >--------
//====< compontent >====
static displayName = Articles.name;
constructor(props) {
//-< react: parameter >
super(props);
//-</ react: parameter >
//--< react: variables >--
this.state = {
articles: [],
loading: true
};
//--</ react: variables >-
}
//--< component.events >--
componentDidMount() {
this.populateData();
}
//--</ component.events >--
//====</ compontent >====
//========< Fill HTML >======
static renderTable(articles) {
return (
<table className='table table-striped' aria-labelledby="tabelLabel">
<thead>
<tr>
<th>title</th>
</tr>
</thead>
<tbody>
{articles.map(article =>
<tr key={article.IDArticle}>
<td>{article.title}</td>
</tr>
)}
</tbody>
</table>
);
}
//========</ Fill HTML >======
//====< react: render >====
//*set root hmtl, load data, fill data
render() {
//----< prepare document >---
let contents = this.state.loading
? <p><em>Loading...</em></p>
: Articles.renderTable(this.state.articles);
//----</ prepare document >---
//----< fill html >----
return (
<div>
<h1 id="tableLabel" >Articles</h1>
<p>This component demonstrates fetching data from the server.</p>
{contents}
</div>
);
//----</ fill html >----
}
//====</ react: render >====
//====< functions >====
//*load data from web api
async populateData() {
//--------< populateData() >--------
const response = await fetch('api/articles'); //*get web_data from web api as json-string
const data = await response.json(); //*convert json to data
this.setState({ articles: data, loading: false }); //*refresh state of arcticles
//--------</ populateData() >--------
}
//====</ functions >====
//--------</ component: Articles >--------
}
|