Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fetching data from nested array in API React Native

I’m trying to fetch data from a nested array from an API in react native.

so my coding is like this

export default const App= () =>{

    const [data, setData] = useState([]);
    console.log(data);

     //Some fetch code here
    //setData happen here
  
    return  (
       <View >
        <Text >fetched data</Text>
          <Text>{data?.data?.items?.newOne?.a}</Text>
          <Text>{data?.data?.items?.newOne?.b}</Text>
          <Text>{data?.data?.items?.newOne?.c}</Text>
       </View>
    )
};

and the supposed array is like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

arr = { code: 1, data:{ items:[{newOne:{a:2, b:4, c:6}, id:00},], totality: 1}}

can someone tell me what I’m doing it wrong here? I’m new to js and also react native. I received blank whenever I run this code.

>Solution :

items is an array, so you have to access it as one

return  (
   <View >
    <Text >fetched data</Text>
      <Text>{data?.data?.items[0]?.newOne?.a}</Text>
      <Text>{data?.data?.items[1]?.newOne?.b}</Text>
      <Text>{data?.data?.items[2]?.newOne?.c}</Text>
   </View>
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading