English 中文(简体)
How to use Axios to fetch data from Google Places API in React Native and display it in UI?
原标题:

Trying to get a restaurant name from Google Places API.

First page displays the list of restaurants, and when user selects one, it passes place_id as an id value to a second page (that I m currently working on).

I managed to grab the data and it displays in console line 23 console.log(result); displays data in console

However, in line 36 {result.name} displays nothing. As I set useState(null), I wonder if it empties it before it gets to the ?

import React, {useState, useEffect} from  react ;
import { View, Text, StyleSheet, Flatlist, Image } from  react-native ;
import axios from "axios";
import { FlatList } from  react-native-gesture-handler ;

const GResultsShowScreen = ({navigation}) => {
    
    const [result, setResult] = useState(null);
    const baseURL =  https://maps.googleapis.com/maps/api/place/details/json?key=MYAPIKEY ;
    const id = navigation.getParam( id );
    const getResult = async (id) => {

    const response=  await axios.get(`${baseURL}`, {
            params: {
                        place_id: id
                    }}
        );

        setResult(response.data);
        
    };

    console.log(result);

    useEffect(() => {
        getResult(id);
    }, []);
    
   
    if (!result) {
         return console.log( nodata );
        }
    
    return (
    <View>       
         <Text>test {result.name} </Text>  
    </View>
    );
};

const styles = StyleSheet.create({});

export default GResultsShowScreen;type here

and this is the console output by console.log(result); so at this point there is data in result

[console output] console output in plain textview

问题回答

暂无回答




相关问题
Async Storage can t retrive data

I m currently creating an app using react native and async storage, but there s a problem when I add the new data, it can t show it on the screen until I ctrl+s the code file, but I want that when I ...

React JS How to submit a form and THEN navigate to new page?

I want to preface this by saying I am completely brand new to web development and am trying to add features to legacy code (and honestly I am not sure what the entire tech stack is)! Thank you for any ...

React Hook Form Error on custom Input component

I am having a problem when I use react hook form + zod in my application, in short the inputs never change value and I get the following error in the console: Warning: Function components cannot be ...

HandleClick not being recognized as a prop

I m trying to get my menu to show when I click the sidebar, but I m getting an error "react-dom.development.js:86 Warning: React does not recognize the handleClick prop on a DOM " import { ...

Is there an equivalent to localStorage in React Native?

I d like to implement the equivalent to the javascript localStorage in my React Native app. But I m unsure how to set. To function how I want it to, I would like the localStorage to be stored every ...

热门标签