Prompt in React Native

Learn how to create and personalize Prompt Alerts in React Native applications through our step-by-step guide.

In this tutorial, we'll show a quick way to request user information using Prompts.

What is the Prompt?

Alert.prompt(), in React Native, is used to get information from a user in a native popup, similar to Alert.alert(). This option is only available on iOS devices.

Types of prompts

  • plain-text - Plain text input prompt (default)
  • secure-text - Secure text input prompt
  • login-password - Login and password prompt

Prompt with Plain Text Input

Similar to Alert.alert(), the first 3 arguments are the same: title, message, and buttons. The next argument is used to provide the prompt type.

iOS Screenshot showing a plain text prompt in a React Native app

import { Alert } from "react-native";

Alert.prompt(
  "Username",
  "Enter your new username below",
  [
    { text: "Cancel", style: "destructive", onPress: () => {} },
    {
      text: "Submit",
      onPress: (username) => {
        // update username
      },
    },
  ],
  "plain-text"
);

Prompt with Secure Input

Secure prompt, just like secure TextInput, is usually used for password fields.

iOS Screenshot showing a secure text prompt in a React Native app

It is common for mobile apps to prompt user enter their password when performing high-security actions (like changing emails) to prevent account fraud.

Alert.prompt(
  "Confirm Password",
  "Enter your current password for confirmation",
  [
    { text: "Cancel", style: "destructive", onPress: () => {} },
    {
      text: "Confirm",
      onPress: (password) => {
        // await verifyPassword(password)
      },
    },
  ],
  "secure-text"
);

Prompt with Login and Password

Lastly, Apple has created a way to enter both username and password in a single prompt.

iOS Screenshot showing a login and password prompt in a React Native app

Alert.prompt(
  "Sign In",
  "Enter your credentials to log in:",
  [
    { text: "Cancel", style: "destructive", onPress: () => {} },
    {
      text: "Sign In",
      onPress: (credentials) => {
        const { login, password } = credentials;

        // Authenticate user with provided credentials
        // await authenticateUser(login, password)
      },
    },
  ],
  "login-password"
);

Setting default value

If you need to provide a default value for the prompt, you can pass it as a 5th argument after the prompt type as shown below:

import { Alert } from "react-native";

const defaultValue = `withframe`;

Alert.prompt(
  "Username",
  "Enter your new username below",
  [
    { text: "Cancel", style: "destructive", onPress: () => {} },
    {
      text: "Submit",
      onPress: (username) => {
        // update username
      },
    },
  ],
  "plain-text",
  defaultValue
);

We hope you enjoyed this content and now better understand how to use Prompt in React Native.

Never miss a beat

Stay in the loop with our latest updates and offers - no spam, just the good stuff!