-1
I am new in the area and know how I can do to have a dynamic consumption of the Rest api in React. Currently my query is fixed and my idea would be to take the value that the user type for query, how can I pass this information via parameter?
import axios from 'axios';
const api = axios.create({
baseURL: 'https://localhost:44340/api'
});
export default api;
My component
import React, { Component } from 'react';
import api from '../../../SolicitacaoApi';
import './style.css'
class SolicitacaoTable extends Component {
state = {
solicitacao: [],
}
async componentDidMount() {
const response = await api.get('solicitacao?protocolo='+1000250170);
this.setState({ solicitacao: response.data });
}
render() {
const { solicitacao } = this.state;
return (
<div>
<div className="main-table">
<table className="table-wrapper">
<thead>
<tr>
<th>Protocolo</th>
<th>Titular</th>
<th>Email</th>
<th>Data de inclusão</th>
<th>Telefone</th>
<th>Tipo de produto</th>
<th>Tipo de emissão</th>
<th className="column-status">Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>{solicitacao.protocolo}</td>
<td>{solicitacao.titularNome}</td>
<td>{solicitacao.email}</td>
<td>{solicitacao.dtInclusao}</td>
<td>{solicitacao.telefone}</td>
<td>{solicitacao.tipoProduto}</td>
<td>{solicitacao.tipoEmissao}</td>
<td>
<div className={'status status-'+solicitacao.idStatusSolicitacao}>
{solicitacao.statusSolicitacao}
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
);
};
};
export default SolicitacaoTable;
The question is unclear, where does the value come from? What exactly is the problem? You can use the
componentDidUpdate
to observe changes in the state or props and perform the request when desired, since thecomponentDidMount
will only be executed once– Rafael Tavares