skip to Main Content

Please help need to insert variable into the return
export default function AboutScreen({ route }) {

const { name } = route.params;
alert(name)

 state = {
        text: '',
        stored1Value: '',
        storedValue: '',
        store1Value: '',

        storeValueU: '',
        storeValueUU: '',
        storeValueUUU: '',
        storeValueUUUU: '',
        storeValueUUUUU: '',
    }

axios.get('https://cookiebytes.ca/google-login-php/Jacob.php?name=' + name)
            .then(response => {
                alert(response.data)
            })
            .catch(error => {
                console.log(error)
            });

return (
        <View>
        <Text>AboutScreen {response}</Text>
        </View>
    );
}

How to get the variable from the response into the return ?!

2

Answers


  1. You should use state to store the response data and then render it.

    import React, { useState, useEffect } from 'react';
    import { View, Text } from 'react-native';
    import axios from 'axios';
    
    export default function AboutScreen({ route }) {
        const { name } = route.params;
        const [response, setResponse] = useState('');
    
        useEffect(() => {
            axios.get('https://cookiebytes.ca/google-login-php/Jacob.php?name=' + name)
                .then(response => {
                    setResponse(response.data);
                })
                .catch(error => {
                    console.log(error);
                });
        }, [name]);
    
        return (
            <View>
                <Text>AboutScreen {response}</Text>
            </View>
        );
    }
    

    I used useEffect to perform the Axios request when the component mounts or when the name parameter changes. The response data is stored in the response state variable and then displayed in the Text component within the return statement.

    Login or Signup to reply.
  2. Instead of the

    state = {}
    

    You are using now, you will want to create an object that matches the return type.

    So, if, for example, the return is a string:

    const [return, setReturn] = useState("");
    

    And then in your get call

    axios.get('https://cookiebytes.ca/google-login-php/Jacob.php?name=' + name)
                .then(response => {
                    setReturn(response.data);
    
                })
                .catch(error => {
                    console.log(error)
                });
    

    And then you can access the data in the return state variable.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search