1

So i'm new to react native and i'm trying to display nested elements of a Json fetched data, but i can't figure out how to do it. After using react hooks in the fetch api:

export default function MedProfilScreen({ route }) {
  //const {id,name,specialite,work}=route.params;
  const [data, setData] = useState([]);
  useEffect(() => {
    fetch("http:......")
      .then(response => response.json())
      .then(res => {
        setData(
          res);
        console.log(res);
      })
      .done();
  },[]);
}

i got this reponse:

Array [
  Object {
    "code": "12459552",
    "id": 7,
    "name": "Dr xavier vilan",
    "speciality": "Cardio",
  },
  Object {
    "education": Array [
      Object {
        "date_debut": "2020-02-07",
        "date_end": "2020-02-06",
        "diploma": "asmaa",
        "school": "asmaa",
        "city": "vullez",
      },
      ]}
]

so i want to display for example: name: Dr xavier.. diploma: asmaa

return(
<View>  name: Dr xavier.. diploma: asmaa            </View>
)

anyone have an idea how to do it please.thank you

4 Answers 4

1

I don't know it this is what you are looking for, but I'll give it a try:

Code:

const renderText = () => {
    // as soon as we received the data, we will display it
    if (data.length > 0){
      return (
         <Text> name: {data[0].name} diploma: {data[1].education[0].diploma}</Text>
      )
    }
}
return (
  <View>
     {renderText()}
  </View>
);

Output:

output

Demo:

https://snack.expo.io/rkbsDdoU8

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

2 Comments

Yes this is what i need but i just get typo error: data[0].name is not an object
@AymenAymen I've updated my answer and the link to the demo
1

Since you already saved your response inside data your data should like this,

data = [
  {
    code: "12459552",
    id: 7,
    name: "Dr xavier vilan",
    speciality: "Cardio"
  },
  {
    education: [
      {
        date_debut: "2020-02-07",
        date_end: "2020-02-06",
        diploma: "asmaa",
        school: "asmaa",
        city: "vullez"
      }
    ]
  }
]

Now you can access name & diploma as below,

name: data[0].name
diploma: data[1].education[0].diploma

Hope this helps you. Feel free for doubts

1 Comment

thank you for your reply, Tim's answer above fixed it!
0

try this:

return (
   <View>{JSON.stringify(data, null, 2)}</View>
)

1 Comment

it just displayed the consol log of res on my app screen
0

Try this referring to the items that you want

return(
     <View>  
          <Text> name: {data[0].name} diploma: {data[1].education[0].diploma}         
           </Text>
     </View>
)

1 Comment

thank you for your reply, Tim's answer above fixed it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.