я впервые пишу код в Javascript / React JS, поэтому я не уверен, что здесь не так.getLicensees () отправляет запрос GET в мой API и возвращает всех лицензиатов.Это работает до сих пор, также журналы консоли работают и печатают правильное значение.
constructor(props) {
super(props);
this.state = {licensees: []};
let licenseeResponse = LicensingService.getLicensees()
.then(licensees => {
this.state.licensees = licensees;
console.log("component", licensees);
console.log(licensees[0].city);
});
}
Я пытаюсь сгенерировать таблицу из всей информации внутри моего объекта лицензиатов.Но я не могу использовать this.state.licensees[0].city
внутри моего метода render ().
render() {
return (
<main id="content">
<div id="head">
<h4 className="headline">
Liste aller Lizenznehmer
</h4>
<div className="licenseeTable">
<table>
<tr>
<th>Lizenz nehmer</th>
<th>Aktuelles Zertifikat</th>
<th>Details</th>
</tr>
<tr>
<td>{this.state.licensees[0].city}</td>
<td>test2</td>
<td>test3</td>
</tr>
</table>
</div>
</div>
</main>
);
}
как я могу сделать это правильно?
- Мое решение:
componentDidMount() {
console.log('component did mount..', this);
LicensingService.getLicensees()
.then(licensees => {
this.setState({licensees});
console.log(licensees);
});
}
...
{
this.state.licensees.map(license => {
return <tr>
<td>{license.company}</td>
<td>{license.testCertificate.toString()}</td>
<td>{license.city}</td>
</tr>
})
}
this.setState ({licensees}) - это правильный способ присвоения значения объекту состояния.