React Native - Alert


Advertisements


In this chapter, we will understand how to create custom Alert component.

Step 1 - Home

src/components/home/Home.js

import React from 'react'
import AlertExample from './AlertExample.js'

const Home = () ⇒ {
   return (
      <AlertExample />
   )
}
export default Home

Step 2 - AlertExample.js

We will create a button for triggering the showAlert function.

src/components/home/AlertExample.js

import React from 'react'
import { Alert, Text, TouchableOpacity, StyleSheet } from 'react-native'

const AlertExample = () ⇒ {
   const showAlert = () ⇒ {
      Alert.alert(
         'You need to...'
      )
   }
   return (
      <TouchableOpacity onPress = {showAlert} style = {styles.button}>
         <Text>Alert</Text>
      </TouchableOpacity>
   )
}
export default AlertExample

const styles = StyleSheet.create ({
   button: {
      backgroundColor: '#4ba37b',
      width: 100,
      borderRadius: 50,
      alignItems: 'center',
      marginTop: 100
   }
})

When you click the button, you will see the following −

React Native Alert

Advertisements