Lesson 18 of 60 – Button Component in React Native
30%

Button Component in React Native

The Button component is a simple built-in React Native component used to create buttons that users can press to perform an action. It is useful when you need a basic button without creating a custom design.

Note: React Native also provides Pressable and TouchableOpacity for more customized interactive interfaces. The Button component is best suited for simple button requirements.

1. What is Button?

Button is a built-in React Native component used to create a simple interactive button.

import { Button } from "react-native";

When the user presses the button, the function provided to onPress is executed.

2. Importing Button

Button is imported from the react-native package.

import { Button } from "react-native";

You can import it with other components as well.

import {
    View,
    Text,
    Button
} from "react-native";

3. Basic Button Syntax

<Button
    title="Click Me"
    onPress={() => {
        alert("Button Pressed");
    }}
/>

The title prop specifies the text displayed on the button.

4. title Prop

The title prop specifies the button's displayed text.

<Button
    title="Submit"
/>

For a useful button, you normally provide an onPress handler as well.

5. onPress Prop

The onPress prop defines what should happen when the user presses the button.

function handlePress() {

    alert("Welcome!");

}

<Button
    title="Welcome"
    onPress={handlePress}
/>

6. Button inside View

Buttons are commonly placed inside View containers to create screen layouts.

<View>

    <Text>
        Student Dashboard
    </Text>

    <Button
        title="Open Dashboard"
        onPress={() =>
            alert("Dashboard")
        }
    />

</View>

7. Button with Alert

A Button can call an alert when it is pressed.

import {
    Button,
    Alert
} from "react-native";

<Button
    title="Show Message"
    onPress={() => {

        Alert.alert(
            "Message",
            "Hello from React Native"
        );

    }}
/>

8. Button with a Function

Instead of writing the function directly inside onPress, you can create a separate function.

function handleLogin() {

    console.log("Login button pressed");

}

return (
    <Button
        title="Login"
        onPress={handleLogin}
    />
);

9. Button with Parameters

You can pass data to a function using an arrow function.

function showStudent(name) {

    console.log(name);

}

<Button
    title="Student"
    onPress={() =>
        showStudent("Rahul")
    }
/>

10. color Prop

The color prop can be used to specify the button's color on supported platforms.

<Button
    title="Submit"
    color="blue"
    onPress={() =>
        alert("Submitted")
    }
/>

The exact appearance of the color can vary between platforms.

11. disabled Prop

The disabled prop can prevent the user from pressing the button.

<Button
    title="Submit"
    disabled={true}
    onPress={() =>
        alert("Submitted")
    }
/>

When disabled, the button cannot perform the press action.

12. Dynamic disabled State

The disabled state can be controlled using React state.

const [disabled, setDisabled] =
    useState(false);

<Button
    title="Submit"
    disabled={disabled}
    onPress={() => {
        console.log("Submitted");
    }}
/>

13. Button with State

A Button can update state when the user presses it.

import { useState } from "react";

const [count, setCount] =
    useState(0);

return (
    <View>

        <Text>
            Count: {count}
        </Text>

        <Button
            title="Increase"
            onPress={() =>
                setCount(count + 1)
            }
        />

    </View>
);

14. Creating a Counter

The Button component can be used to create a simple counter.

const [count, setCount] =
    useState(0);

<Text>
    {count}
</Text>

<Button
    title="Add"
    onPress={() =>
        setCount(count + 1)
    }
/>

Every press updates the state and causes the displayed value to change.

15. Multiple Buttons

A screen can contain multiple Button components.

<View>

    <Button
        title="Login"
        onPress={() =>
            console.log("Login")
        }
    />

    <Button
        title="Register"
        onPress={() =>
            console.log("Register")
        }
    />

    <Button
        title="Help"
        onPress={() =>
            console.log("Help")
        }
    />

</View>

16. Button Spacing

The built-in Button does not provide the same level of layout customization as Pressable. You can place it inside a View and use layout styles to control surrounding space.

const styles = StyleSheet.create({

    buttonContainer: {
        marginTop: 20
    }

});
<View style={styles.buttonContainer}>

    <Button
        title="Submit"
        onPress={() =>
            console.log("Submit")
        }
    />

</View>

17. Button in a Form

Button can be used to submit or process form data.

<View>

    <Text>
        Name
    </Text>

    <TextInput
        placeholder="Enter name"
    />

    <Button
        title="Submit"
        onPress={() => {
            console.log(
                "Form submitted"
            );
        }}
    />

</View>

18. Button with TextInput

The value of a TextInput can be stored in state and processed when the Button is pressed.

const [name, setName] =
    useState("");

<TextInput
    value={name}
    onChangeText={setName}
    placeholder="Enter name"
/>

<Button
    title="Save"
    onPress={() => {
        console.log(name);
    }}
/>

19. Button for Navigation

When a navigation library is configured, a Button can trigger navigation to another screen.

<Button
    title="Open Profile"
    onPress={() =>
        navigation.navigate("Profile")
    }
/>

The exact navigation code depends on the navigation solution and screen configuration used by the application.

20. Button for API Actions

A Button can trigger a function that sends or retrieves data from an API.

async function loadData() {

    const response =
        await fetch(
            "https://example.com/api"
        );

    const data =
        await response.json();

    console.log(data);

}

<Button
    title="Load Data"
    onPress={loadData}
/>

21. Button for Logout

Buttons are commonly used for actions such as logging out.

function handleLogout() {

    console.log("User logged out");

}

<Button
    title="Logout"
    onPress={handleLogout}
/>

In a real application, the logout function may clear authentication data and navigate to a login screen.

22. Button for Delete Action

A Button can trigger a delete operation.

function deleteStudent() {

    console.log(
        "Student deleted"
    );

}

<Button
    title="Delete"
    color="red"
    onPress={deleteStudent}
/>

For important destructive actions, applications should normally ask the user for confirmation before deleting data.

23. Button and Loading State

A loading state can be used to prevent repeated actions while an operation is in progress.

const [loading, setLoading] =
    useState(false);

<Button
    title={
        loading
            ? "Please wait..."
            : "Submit"
    }
    disabled={loading}
    onPress={async () => {

        setLoading(true);

        // Perform operation

        setLoading(false);

    }}
/>

24. Button and Accessibility

Buttons are interactive controls, so their purpose should be clear to users.

Use meaningful button titles such as:

  • Save
  • Submit
  • Login
  • Register
  • Open Profile
  • Logout

Clear labels make the purpose of an action easier to understand.

25. Button vs Pressable

Button Pressable
Simple built-in button Flexible interactive component
Easy to use Requires more styling
Limited visual customization Highly customizable
Uses title prop Can contain custom children
Good for simple actions Good for custom buttons and cards

26. When to Use Button

  • Simple form submission
  • Basic login buttons
  • Simple actions
  • Basic confirmation actions
  • Simple navigation actions
  • Quick prototypes
  • Small applications where advanced button styling is not required

When you need detailed control over layout, pressed states or custom button content, Pressable may be more appropriate.

27. Complete Button Example

import {
    View,
    Text,
    Button,
    StyleSheet
} from "react-native";

export default function App() {

    function handleLogin() {

        alert("Login button pressed");

    }

    return (
        <View style={styles.container}>

            <Text style={styles.title}>
                Student Login
            </Text>

            <View style={styles.button}>

                <Button
                    title="Login"
                    onPress={handleLogin}
                />

            </View>

            <View style={styles.button}>

                <Button
                    title="Register"
                    onPress={() =>
                        alert("Register")
                    }
                />

            </View>

        </View>
    );
}

const styles = StyleSheet.create({

    container: {
        flex: 1,
        justifyContent: "center",
        padding: 20
    },

    title: {
        fontSize: 26,
        fontWeight: "bold",
        textAlign: "center",
        marginBottom: 20
    },

    button: {
        marginTop: 15
    }

});

28. Common Button Mistakes

  • Forgetting to provide an onPress action when one is needed.
  • Using unclear button titles.
  • Allowing users to repeatedly submit the same action during loading.
  • Expecting the built-in Button to provide highly customized layouts.
  • Ignoring disabled states for actions that should temporarily be unavailable.
  • Using too many buttons without clear organization.

29. Best Practices for Button

  • Use clear and meaningful titles.
  • Keep button actions simple and predictable.
  • Use onPress for the required action.
  • Use disabled when the action is temporarily unavailable.
  • Handle loading states for network operations.
  • Use confirmation for important destructive actions.
  • Use Pressable when advanced customization is required.
  • Test buttons on the platforms supported by your application.

30. Complete Button Workflow

Button
 ↓
Set Title
 ↓
Add onPress
 ↓
Write Action
 ↓
Optional State
 ↓
Optional Disabled State
 ↓
Handle Loading
 ↓
Perform Action
 ↓
Update Screen
 ↓
Complete Button Interaction

The Button component is a simple way to add user actions to a React Native application. After learning Button, you can use Pressable when you need more control over the appearance and interaction behavior.

📌 Key Points

  • Button is a built-in React Native component.
  • The title prop defines the button text.
  • The onPress prop defines the action performed after pressing.
  • The color prop can control the button color on supported platforms.
  • The disabled prop can disable the button.
  • Button can work with state, forms and API operations.
  • Button can trigger navigation when navigation is configured.
  • Button is suitable for simple actions.
  • Pressable is more suitable for highly customized interactive UI.

🧠 Quick Quiz

Question: Which prop is used to specify the text displayed on a React Native Button?