I am trying to render a table from data i get via an ajax call. Here is the code:
let tableResult = null;
let tableRows = null;
if(this.state.searchResult === 'records-found') {
tableRows = this.state.transactionsData.content.map((item, index) => {
let date = new Date(item.dateOfPayment);
return (
<tr key={item.id}>
<td>{date}</td>
<td>{item.transactionReference}</td>
<td>{item.merchantCustomerId}</td>
<td>{item.amount}</td>
<td>{item.status}</td>
</tr>
);
});
tableResult = (
<div className="transactions-table-wrapper">
<div className="transactions-table">
<table>
<thead>
<tr>
<td>Date Of Payment</td>
<td>Merchant Reference</td>
<td>Customer Id</td>
<td>Amount</td>
<td>Status</td>
</tr>
</thead>
<tbody>
{tableRows}
</tbody>
</table>
</div>
</div>
);
}
Then table gets rendered like so:
<div className="transitions-search-result">
{tableResult}
</div>
The error I get is "Cannot read property 'content' of null" but I print the state before that line. The value is set
SubTransactions."renderfunction before your ajax request is done (meaning at that time your state isn't set the way you want). Do you have an initial state in your component (i.e.: do you setthis.state = ...on your component'sconstructor) ?