skip to Main Content

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


  1. 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}/>})
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search