Home > Net >  React Native map undefined is not a function
React Native map undefined is not a function

Time:12-17

i'm trying to get data from api but i err got this error 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

CodePudding user response:

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}/>})

CodePudding user response:

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.

  • Related