React Native

Get current User for React Native

Introduction

After implementing user registration and login to your, you need to retrieve the currently logged user data to perform different actions and requests. Since React Native uses AsyncStorage as the local storage, this data can be retrieved using Parse.currentAsync inside your app’s component.

Prerequisites

To complete this tutorial, you will need:

Goal

Get the current user data fetching using Parse for a React Native App.

Step 1 - Retrieving Current User

The method Parse.currentAsync can be used anywhere on your code, after properly configuring your app to use Parse and AsyncStorage. Its response will be your current user’s object (Parse.User) or null if there is no logged-in user currently.

1
2
3
4
5
6
7
8
9
10
const getCurrentUser = async function () {
  const currentUser = await Parse.User.currentAsync();
  if (currentUser !== null) {
    Alert.alert(
      'Success!',
      `${currentUser.get('username')} is the current user!`,
    );
  }
  return currentUser;
};
1
2
3
4
5
6
7
8
9
10
const getCurrentUser = async function (): Promise<Parse.User> {
  const currentUser: Parse.User = await Parse.User.currentAsync();
  if (currentUser !== null) {
    Alert.alert(
      'Success!',
      `${currentUser.get('username')} is the current user!`,
    );
  }
  return currentUser;
};

This method is essential in situations where you don’t have access to your application state or your user data, making it possible to perform relevant Parse requests in any component of your app.

Step 2 - Using Current User in a React Native component

In our previous guides, Parse.currentAsync was already used for testing and inside the HelloUser component. Here is the complete component again:

HelloUser.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import React, {FC, ReactElement, useEffect, useState} from 'react';
import {Text, View} from 'react-native';
import Parse from 'parse/react-native';
import Styles from './Styles';

export const HelloUser = () => {
  // State variable that will hold username value
  const [username, setUsername] = useState('');

  // useEffect is called after the component is initially rendered and
  // after every other render
  useEffect(() => {
    // Since the async method Parse.User.currentAsync is needed to
    // retrieve the current user data, you need to declare an async
    // function here and call it afterwards
    async function getCurrentUser() {
      // This condition ensures that username is updated only if needed
      if (username === '') {
        const currentUser = await Parse.User.currentAsync();
        if (currentUser !== null) {
          setUsername(currentUser.getUsername());
        }
      }
    }
    getCurrentUser();
  }, [username]);

  // Note the conditional operator here, so the "Hello" text is only
  // rendered if there is an username value
  return (
    <View style={Styles.login_wrapper}>
      <View style={Styles.form}>
        {username !== '' && <Text>{`Hello ${username}!`}</Text>}
      </View>
    </View>
  );
};

HelloUser.tsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import React, {FC, ReactElement, useEffect, useState} from 'react';
import {Text, View} from 'react-native';
import Parse from 'parse/react-native';
import Styles from './Styles';

export const HelloUser: FC<{}> = ({}): ReactElement => {
  // State variable that will hold username value
  const [username, setUsername] = useState('');

  // useEffect is called after the component is initially rendered and
  // after every other render
  useEffect(() => {
    // Since the async method Parse.User.currentAsync is needed to
    // retrieve the current user data, you need to declare an async
    // function here and call it afterwards
    async function getCurrentUser() {
      // This condition ensures that username is updated only if needed
      if (username === '') {
        const currentUser = await Parse.User.currentAsync();
        if (currentUser !== null) {
          setUsername(currentUser.getUsername());
        }
      }
    }
    getCurrentUser();
  }, [username]);

  // Note the conditional operator here, so the "Hello" text is only
  // rendered if there is an username value
  return (
    <View style={Styles.login_wrapper}>
      <View style={Styles.form}>
        {username !== '' && <Text>{`Hello ${username}!`}</Text>}
      </View>
    </View>
  );
};

In this case, the Parse.currentAsync method retrieves the username and updates the state variable that is rendered inside the component JSX.

Conclusion

At the end of this guide, you learned how to retrieve the current Parse user data from local storage on React Native. In the next guide, we will show you how to allow your user to reset his password.