Open In App

React Native Button Component

The following approach covers how to use the Button in react-native. For this, we are going to use the Button component. It is basically a clickable component which is used to perform some action when clicked by the user.

Syntax:



<Button
 onPress={}
 title="Button"
 color="#841584"
/>

Props in Button:

Now let’s start with the implementation:



Project Structure: It will look like the following.

Example: Now lets implement the Button. Here we created a Button and when someone clicks on that button the count will change.

App.js




import React , {useState} from 'react';
import { StyleSheet, Text, View , Button } from 'react-native';
  
export default function App() {
  const [count , setcount] = useState(0);
  const changeCount = () => {
    setcount(count + 1);
  }
  return (
    <View style={styles.container}>
        <Text style={styles.text}>{count}</Text>
        <Button
          title={"Click Me"}
          onPress={changeCount}
        />
    </View>
  );
}
  
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
  text : {
    fontSize : 40,
    marginBottom : 30
  }
});

Start the server by using the following command.

npm run android

Output: If your emulator did not open automatically then you need to do it manually. First, go to your android studio and run the emulator. Now start the server again.

Reference: https://reactnative.dev/docs/button


Article Tags :