In this blog, learn Button component in React Native and how to use its properties. Click on the following link for React Native Code setup for Android Apps.
The Button in React Native is used to interact with screen and helps to pass command/action for user by clicking on it.
Button Properties
- title - Represents the text inside the button ex: - Login, Logout, Register, Save.
- color - To change the background color of Button in Android and text color in iOS.
- disabled - To make Button disable. (User cannot perform any action with this component)
- onPress - (Event) To call handler/function.
- Other properties - touchSoundDisabled, accessibilityLabel, testid, hasTVPreferredFocus, nextFocusDown, nextFocusForward, nextFocusLeft, nextFocusRight
Syntax
<Buttontitle="Button Title"onPress={"functionName()"}/>
Example
Button component with title and onPress event.
First Step - Import Button, View, StyleSheet component from React Native library.
import {View, Button, StyleSheet} from"react-native";
Second Step - Add button component in View component of React Native. Add title property and onPress event to button component as well as add style using StyleSheet.
Code
- import React, { Component } from "react";
- import { View, Button, StyleSheet } from "react-native";
- class App extends Component {
- clickHandler = () => {
- alert('Hi, User.');
- }
- render() {
- return (
- <View style={styles.container} >
- <View style={{ paddingTop: 10 }}>
- <Button title="Save" color="green" onPress={this.clickHandler} />
- </View>
- <View style={{ paddingTop: 10 }}>
- <Button title="Register" color="green" disabled={true} onPress={this.clickHandler} />
- </View>
- </View>
- );
- }
- }
- const styles = StyleSheet.create({
- container: {
- margin: 10,
- justifyContent: 'space-between',
- paddingTop: 10
- }
- })
- export default App;


Join the conversation! Your thoughts help the community grow.