Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to retrieve data from useEffect with react native?

I have a service that returns user data:

export const AccountRequest = async () => {
    const token = await retrieveToken();
    try {
        if (token) {
            const response = await fetch("http://192.168.1.65:8000/api/user/me/", {
                method: "GET",
                headers: {
                    Authorization: `Token ${token}`,
                    "Content-Type": "application/json",
                },
            });
            console.log(response);
            return await response.json();
        } else {
            throw new Error(token);
        }
    } catch (error) {
        error("can't retrieve data");
    }
};

And I have a sreen component where I want to display user name and email:

export const SetScreen = ({ navigation }) => {  
    const [query, setQuery] = useState("");
    

    useEffect(() => {
        AccountRequest().then((data) => {
            console.log(data.email);
        });
    }, [query]);

    return (
        <AccountBackground>
            <AccountCover />
            <Title>Instellingen</Title>
            <AccountContainer>
                <Spacer position="top" size="large">
                    <Text variant="label">{query.email}</Text>              
                </Spacer>               
            </AccountContainer>         
        </AccountBackground>
    );
};

and from the useEffect I get the correct data returned: user@user.nl.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But how to return this data propertie in the Text component?

<Text>{query.email}</Text>

Question: how to return the email adsress in the Text component?

>Solution :

create one user state and once the data is retrieved saved it in that state.

Try this

export const SetScreen = ({navigation}) => {
  const [query, setQuery] = useState('');
  const [userData, setUserData] = useState({});

  useEffect(() => {
    AccountRequest().then(data => {
      console.log(data.email);
      setUserData(data);
    });
  }, [query]);

  return (
    <AccountBackground>
      <AccountCover />
      <Title>Instellingen</Title>
      <AccountContainer>
        <Spacer position="top" size="large">
          <Text variant="label">{userData?.email}</Text>
          <Text>{userData?.email}</Text>
        </Spacer>
      </AccountContainer>
    </AccountBackground>
  );
};

I hope this will help you out!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading