-1
I need to fill a table with all the items in my API, but when the process is finished the table returns empty. I did a fetch to access the API and tried to put all the items inside an array within my state called "data" and gave a setState to fill this array with the API values. Then I made a map in data to create a list within that table with each of the filled-in items. But fields return empty and error free...
I found the API that I’m using a little confusing but I’m obliged to use it... I believe the problem is in the naming given in my State array which is data and in the API the Array has no name. I do not know how to proceed.
Please this is an exercise I’m doing to learn React, but I’m stuck, can help me?
The API I’m using is running on the mocked json-server, but the structure is exactly the same as the following link: https://gitlab.com/desafio-conta-simples/developer/-/blob/master/mocks/transacoes.json
Follows the code:
import React, { Component } from 'react'
import { Container, Row, Col, Table} from 'react-bootstrap'
class Extrato extends Component {
constructor(props){
super(props);
this.state = {
dados: []
}
}
componentDidMount(){
fetch("http://localhost:3000/transacoes")
.then(resultado => resultado.json().then(dados => this.setState(dados)))
}
render() {
return (
<Container>
<Table striped bordered hover variant="dark">
<thead>
<tr>
<th>Empresa ID</th>
<th>Data Transação</th>
<th>Valor</th>
<th>Final do Cartão</th>
<th>Tipo de Transação</th>
<th>Descrição da Transação</th>
<th>Estabelecimento</th>
<th>Crédito</th>
</tr>
</thead>
<tbody>
{
this.state.dados.map((item, indice) => {
return (
<tr key={indice}>
<td>{item.empresaId}</td>
<td>{item.dataTransacao}</td>
<td>{item.valor}</td>
<td>{item.finalCartao}</td>
<td>{item.tipoTransacao}</td>
<td>{item.descricaoTransacao}</td>
<td>{item.estabelecimento}</td>
<td>{item.credito}</td>
</tr>
)
})
}
</tbody>
</Table>
</Container>
)
}
}
export default Extrato;
has an error in your jsx ... check must be giving error.
– novic
I checked, there are no errors in the application. When I give the npm start it runs normally only without filling the table and in the console there are also no errors. What I saw was more than two bootstrap imports, but I’ve already deleted and the table is still empty.
– user188554
It may be more suitable for a question than for a comment, but in the case that API Array has no name, in the videos I’ve seen all API Arrays have name, in which case the API Array has no name, as I indicate that that State Array should receive the value of the API Array?
– user188554