React Native On Press Event Example


Share:
Published By: Admin, Published On: Apr 01, 2021, Category: React Native

Hi Guys,

In this blog, I will learn you how to create on press event in react native. You can easily create on press event in react native. First i will create new fuction sayHello, after I will make On Press event using onPress attitude add in tag react native.

Here, I will give you full example for simply display On Press event using react native as bellow.

Step 1 - Create project

In the first step Run the following command for create project.

expo init OnPressEvent 
Step 2 - App.js In this step, You will open App.js file and put the code.
import React, { Component } from 'react';
import { Button, StyleSheet, View , Text} from 'react-native';

export default class ButtonBasics extends Component {
  _sayHello() {
    alert('Hello')
  }

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.textinfo}>
        React Native onpress Event Example - MyWebtuts.com
        </Text>
        <View style={styles.buttonContainer}>
          <Button
            onPress={this._sayHello}
            title="Press Me"
            color="#FFb606"
          />
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
   flex: 1,
   justifyContent: 'center',
  },
  buttonContainer: {
    margin: 20,
    width: 320,
   justifyContent: 'center',
  },
  textinfo:{
    fontSize: 25,
    margin: 20,
  },
  alternativeLayoutButtonContainer: {
    margin: 20,
    flexDirection: 'row',
    justifyContent: 'space-between'
  }
});
Step 3 - Run project

In the last step run your project using bellow command.

npm start
Output

It will help you...

Share: