0

== UPDATE ==

I have a problem with the feth of a JSON, I'm getting a JSON from google maps (this is the code) but I find it undefined.

    const [isLoading, setLoading] = useState(true);
    const [info, setInfo] = useState([]);

    const getInfo = () => {
        fetch(`https://maps.googleapis.com/maps/api/place/nearbysearch/json?place_id=${resturant.id}&$"'privatekey'"`)
          .then((response) => response.json())
          .then((json) => setInfo(json))
          .catch((error) => console.error(error))
          .finally(() => setLoading(true));
    }
    useEffect(() => {
        setLoading(false);
        getInfo();
    }, []);

    return(
        <View style={style.card}>
            {
            !isLoading ? <Text>LOADING</Text> :
                <View style={{
                flexDirection: "row",
                justifyContent: "space-between",
                marginTop: 10
            }}>
                <Text>{info.result.name}</Text> //this is the problem
            </View>
            }
        </View>
    )

JSON return

Error Message

2
  • 1
    Instead of providing screenshots provide the actual snippets of your code. Also, you have uploaded the code screenshots twice and neglected to upload the error message. Please update your question Commented Aug 23, 2022 at 18:15
  • I have updated, thanks Commented Aug 23, 2022 at 20:38

1 Answer 1

1
const [isLoading, setLoading] = useState(false);
    const [info, setInfo] = useState([]);

    const getInfo = () => {
        fetch(`https://maps.googleapis.com/maps/api/place/nearbysearch/json?place_id=${resturant.id}&$"'privatekey'"`)
          .then((response) => response.json())
          .then((json) => setInfo(json))
          .catch((error) => console.error(error))
          .finally(() => setLoading(false));
    }
    useEffect(() => {
        setLoading(true);
        getInfo();
    }, []);

    return(
        <View style={style.card}>
            {
            isLoading ? <Text>LOADING</Text> :
                <View style={{
                flexDirection: "row",
                justifyContent: "space-between",
                marginTop: 10
            }}>
                <Text style={{fontSize: 16, fontWeight: "bold"}}>{resturant.nome}</Text>
                <Text style={{fontSize: 16, fontWeight: "bold"}}>{resturant.id}</Text>
                <Text>{info?.result?.name}</Text> //Update to utilize optional chaining
            </View>
            }
        </View>
    )

I am assuming the some of the objects do not contain the same level of information. Because of this, opt to use optional chaining to account for some of your result objects either missing or not containing a name like I am doing above.

EDIT

instead of optional chaining you could also update your initial state of your isLoading state to be true initially, there is no reason for it to be initially false. This will in turn ensure that your JSX will not render the info.result.name until the fetch request is completed.

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

4 Comments

I think this is good solution. JSX is rendered before data is fetched. That's why info is undefined.
@k0uh0t ah you’re right, didn’t realize initial state was an empty array
thanks for your answers, so what would the optimal solution be?
@TommasoCeredi I have updated my answer to show two options that should work

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.