2

I'm trying to get data from API but. I'm getting this error Error Image.

Here is my code.

const [datas, setDatas] = useState(" ");

const res = async () => {
 const response = await axios.get("http://hasanadiguzel.com.tr/api/kurgetir");
 setDatas(response.data.TCMB_AnlikKurBilgileri);
};

datas.map((item) => {
 return (
  <KurCard
   title={item.Isim}
   alis={item.BanknoteBuying}
   satis={item.BanknoteSelling}
  />
 );
});

How can I solve this?

I'm trying to map() datas, because I need it

2 Answers 2

1
Hi @n00b,

The data that datas is initially being set to an empty string, which does not have a map method. First, you need an empty array instead of an empty stringuseState([]). Now you can map.

const [datas, setDatas] = useState([]);

const res = async () => {
  const response = await axios.get('http://hasanadiguzel.com.tr/api/kurgetir');
  setDatas(response.data.TCMB_AnlikKurBilgileri);
};

{datas.length > 0 &&
  datas.map((item) => {
    return <KurCard title={item.Isim} alis={item.BanknoteBuying} satis={item.BanknoteSelling}/>
  })
}

make sure you data. it has a length greater than 0 before trying to map over it.

Sign up to request clarification or add additional context in comments.

Comments

0

Assuming your API request is valid, you would need to actually return something from the component itself and not just the array:

return datas.map((item) => {return <KurCard title={item.Isim} alis={item.BanknoteBuying} satis={item.BanknoteSelling}/>})

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.