0

I have a problem with React-Native. I want to show data from Django to React-Native, but I can't show one data, this is the code:

import React, { useState, useEffect } from "react";
import { StyleSheet, View, Text, Image, FlatList } from "react-native";
import client from "./../../api/client";


const DetailView = ({navigation, route}) => {
  const [detail, setDetail] = useState("");
  const { objurl } = route.params;

  const getDetail = async (url) => {
    try {
      const response = await client.get(url);
      if (!response.ok) {
        setDetail(response.data);
      }
    } catch (error) {
      console.log(error);
    }
  };
  
  useEffect(()=>{ getDetail(objurl); }, [])

  console.log(detail.habilidad_usuario.nombre_habilidad);

  return (
      <View style={styles.center}>
        <Image
        style={styles.usuarioImage}
        source={{
          uri: detail.foto_usuario,
        }}
      />
        <Text style={styles.name}>{detail.nombre_usuario} {detail.apellido_usuario}</Text>
        <Text style={styles.name}>{detail.habilidad_usuario.nombre_habilidad}</Text>
        <Text style={styles.description}>{detail.descripcion_usuario} </Text>
        <Text style={styles.body}>Dirección: {detail.direccion_usuario}  </Text>
        <Text style={styles.body}>Celular: {detail.id_usuario} </Text>
        <FlatList
        data={detail.usuario_comentado}
        keyExtractor={(item) => item.id.toString()}
        renderItem={({ item }) => {
          return (
            <Text style={styles.body}>Comentario: {item.contenido_comentario} </Text>
          );
        }}
        />
        

      </View>
    );
  }

The problem is in the line:

<Text style={styles.name}>{detail.habilidad_usuario.nombre_habilidad}</Text>

and It's the result:

enter image description here

The others data is rendering very well.

The data from Django Rest is: enter image description here

  • The data is simply not there yet on first render, no? So detail is an empty string, as long as you aren't really going 2 levels deep, it works (it returns undefined on all but `.length` property or any functions you might accidentally type). So, don't render the fields until the data actually got loaded :) – Icepickle Mar 10 '21 at 13:26
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Icepickle Mar 10 '21 at 13:30
  • Thank you, but my code has async and await. When I save the changes the app works, but when I use another functionality and then return to this funcionality the same error is showed. – Gerson Orihuela Maita Mar 11 '21 at 01:44
  • It doesn't really wait, the first render will still have your empty string before the data would arrive, you could try it by setting `useState({ habilidad_usuario: {} })` like this, and I believe it would work – Icepickle Mar 11 '21 at 09:46
  • Thank you very much @Icepickle, It works. – Gerson Orihuela Maita Mar 12 '21 at 00:13

0 Answers0