skip to Main Content

I am trying to create a simple function which can be called in a React Native Screen, which returns an array with data.

I am importing the function here:

import React from 'react'
...
import { getvals } from '../components/Data_funks';

export default function ProjectsScreen({ route, navigation }) {

      const { data }    = route.params;
      const mail        = data.item.kontakte_mail;

    // this is how I call the function:
    getvals(data.item.projekt_bauleiter).then(response => console.log(response));

The array is displayed in the console but I cannot access the data of it. This is the function:

import React from 'react'
import { Image, StyleSheet } from 'react-native'
import clientConfig from '../../client-config';


export function getvals(user_id){
          const siteUrl = clientConfig.siteUrl + '/wp-json/zep/v1/' + user_id;
    return fetch(siteUrl,
    {
        method: "GET",
        headers: {
             Authorization: 'Bearer '+localStorage.getItem( 'token' )
        },
    })
    .then((response) => response.json())
    
    .then((responseData) => {
        
    const error = responseData.data.error_code; 

        
    if(error == 0){     
        
      return responseData;
        
    }else{
        
        return 'error';
    }
        
        
    })
    .catch(error => console.warn(error));
  }

2

Answers


  1. I dont know if this is a typo.. but it seems that you log the string ‘response’

    getvals(data.item.projekt_bauleiter).then(response => console.log('response'));
    
    Login or Signup to reply.
  2. You could save the response data in a state.

    export default function ProjectsScreen({ route, navigation }) {
    
          const { data }    = route.params;
          const mail        = data.item.kontakte_mail;
          const [response, setResponse] = useState()
    
          useEffect(() => {
              const init = async () => {
                  const response = await getvals(data.item.projekt_bauleiter)
                  setResponse(response)
              }   
              init()
          }, [route.params])
    
          console.log(response)
    
    ...
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search