[英]How can I show Alert message without clicking a button in React Native app?
I want to show an alert message immediately when I visit a specific page in my react native app.当我访问我的反应原生应用程序中的特定页面时,我想立即显示一条警报消息。 There should not be any Alert button, Alert should appear automatically whenever I visit the page.
不应该有任何警报按钮,只要我访问该页面,警报就会自动出现。 Thanks for your support.
谢谢你的支持。
import React, { useEffect, useState } from "react";
import {View, Text, StyleSheet, Alert} from 'react-native'
import { Ionicons } from '@expo/vector-icons';
const AlertTest = ({ navigation }) =>{
return (
<View style={{
backgroundColor: '#f1f1f1',justifyContent: 'center', alignItems: 'center',}}>
<Ionicons color="black" name="tv" size={100}/>
<Text>Hello World </Text>
</View>
);}
export default AlertTest;
import React, {useEffect} from "react";
import {View, Text, ToastAndroid} from "react-native";
import {Ionicons} from "@expo/vector-icons";
const AlertTest = ({navigation}) => {
useEffect(() => {
alert("Some Alert");
// OR
ToastAndroid.show("Some Alert Toast", ToastAndroid.SHORT);
}, []);
return (
<View
style={{
backgroundColor: "#f1f1f1",
justifyContent: "center",
alignItems: "center",
}}>
<Ionicons color="black" name="tv" size={100} />
<Text>Hello World </Text>
</View>
);
};
export default AlertTest;
Please find the below code:请找到以下代码:
import React, { useEffect, useState } from "react";
import {View, Text, StyleSheet, Alert} from 'react-native'
import { Ionicons } from '@expo/vector-icons';
const AlertTest = ({ navigation }) =>{
useEffect(() => {
Alert.alert('Your alert msg here!')
},[])
return (
<View style={{
backgroundColor: '#f1f1f1',justifyContent: 'center', alignItems: 'center',}}>
<Ionicons color="black" name="tv" size={100}/>
<Text>Hello World </Text>
</View>
);}
export default AlertTest;
useEffect with [ ] will render one time only on-screen load.带有 [ ] 的 useEffect 将仅在屏幕加载时渲染一次。
Hope it will help you!希望对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.