'React Native create helper

I am very new to react native and try to create my first app. I now want to create functions to get data from my api. How can I create a file with several functions (like in php) to get the data from my api.

For example:

  • get_user()
  • get_customer(customer_id)
  • get_adress(user_id) and a lot more getter.

New helper function: (file=Data_funks.js):

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


const helpers = {
    
    helper1: function(user_id){
        
         const get_bauleiter = async () => {
            try {   
                  const siteUrl = clientConfig.siteUrl + '/wp-json/zep/v1/' + user_id;
                  const response = await fetch(siteUrl, {
                        method: 'GET',
                        headers: {
                             Authorization: 'Bearer '+localStorage.getItem( 'token' )
                        }
                  })

            if (response.status === 200) {  


                                let json = await response.json();
                                const array = json.data;
                                const error = array.error_code; 
                                const my_array=array.data_values[0][0];
                

                    if(error == 0){ 


                        var result =Object.entries( my_array );
//var result = Object.entries(my_array).map((key,values) => [key, my_array[key]]);
        
                        await localStorage.setItem( 'test', result);

                        return result;
                        
                        
                    }else{
                        
                        setData(1201);
                        
                    }

                }else {
                    
                        setData(1200);
                    
                }

                    } catch (err) { 
                        
                         console.log(err);  
                        
                    }
            }   
             
        get_bauleiter();     
             
        } //end helper1
    
} // end helpers


export default helpers;

Now I use to get the Array in this function here but it doesen´t work:

...
import React from 'react'
import {Alert, View, Text, Modal, StyleSheet, ImageBackground, ScrollView, TouchableOpacity, Linking} from 'react-native'

import Background from '../components/Background_2';
import BackButton from '../components/BackButton';
import funcs from '../components/Data_funks';

import Icon2 from 'react-native-vector-icons/Ionicons';



//##################### ICONS Importieren ##################### 
import AppLoading from 'expo-app-loading';
import { useFonts } from 'expo-font';
import { createIconSetFromIcoMoon } from '@expo/vector-icons';


const Icon = createIconSetFromIcoMoon(
  require('../../assets/icomoon/selection.json'),
  'IcoMoon',
  'zepra_icons.ttf'
);

//#############################################################




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

      const { data }    = route.params;
      const mail        = data.item.kontakte_mail;
      const test        = funcs.helper1(data.item.projekt_id);
      const testData    = localStorage.getItem( 'test' ); 
    
        var myArray = testData.split(',');
    
    
        var myArray = myArray.filter(n => n);

        console.log(myArray);

    

What I receive is this:

Array [ "contact_id", "1", "contact_unternehmen", "47", "contact_anrede", "Frau", "contact_titel", "Dr.", "contact_infos", "Testinfo", "contact_vorname", "Olivia", "contact_nachname", "Candal", "contact_position", "Öffentlichkeitsarbeit", "contact_strasse", "Pariser Straße", " 2", "contact_zipcode", "589885", "contact_ort", "Limburg an der Lahn", "contact_land", "Deutschland", "contact_telefon", "+49 (0)654545454454454545", "contact_telefax", "+49 (0)654545454454454545", "contact_mobil", "+49 (0)654545454454454545", "contact_mail", "[email protected]", "contact_buero", "VDT e.V.", ]




Solution 1:[1]

Check this demo, where I have returned different types of data: Helper Demo

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Harsh Patel