React Native Alert 怎么用?弹窗提示该怎么实现?

文章导读
Previous Quiz Next 在本章中,我们将学习如何创建自定义的 Alert 组件。
📋 目录
  1. 步骤 1: App.js
  2. 步骤 2: alert_example.js
A A

React Native - Alert



Previous
Quiz
Next

在本章中,我们将学习如何创建自定义的 Alert 组件。

步骤 1: App.js

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

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

步骤 2: alert_example.js

我们将创建一个按钮,用于触发 showAlert function。

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
   }
})

输出

React Native Alert

当你点击按钮时,你将看到以下内容 −

React Native Alert Button