0
Hello, I searched on the subject and saw that it may be an error that may be due to not receiving an array. However, I put to be returned an array in the API, can be consulted here https://agendaapplp3.herokuapp.com/contatos
only that in the part of React appears this error, "TypeError: contacts.map is not a function".
follows the code:
const ListContacts: React.FC = () => {
const [contacts, setContacts] = useState <ContactsProps[]>([]);
async function getContacts(){
const response = await api.get<ContactsProps[]>("/contatos");
setContacts(response.data);
}
useEffect(()=>{
getContacts();
},[]);
the error is in this part below.
{contacts.map((contact)=>(
<tr>
<td>{contact.id}</td>
<td>{contact.nome}</td>
<td>{contact.email}</td>
<td>{contact.telefone}</td>
<td>{contact.idlocal}</td>
<td>{contact.idtipocontato}</td>
</tr>
to connect the api, I used Axios
import axios from "axios";
const api = axios.create({
baseURL:"https://agendaapplp3.herokuapp.com"
})
export default api;
I’m new to React, which may be?
You shouldn’t put in your
setContactsasresponse.data.results? 'cause yeah yourresultsand an array.– Cmte Cardeal
placed and appeared to me that Property 'Results' does not exist on type 'Contactsprops[]'. TS2339 If you get ahead of something, my Contactsprops[] looks like this: interface Contactsprops{ id:number; name:string; email:string; phone:string; idlocal:number; idtype;}
– GabrielCastilho