简体   繁体   English

如何在不单击 React Native 应用程序中的按钮的情况下显示警报消息?

[英]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.

相关问题 通过单击按钮显示消息,但我如何通过单击按钮隐藏此消息 - show message by clicking button but how i can hide this message by clicking the button 如何在 react-native 中设置警报按钮? - how can i make on alert button in react-native? 如何在React Native中单击按钮时警告随机文本 - how to alert random texts upon clicking button in react native 如何通过单击反应本机按钮将数据发送到 webview - How can I send a data to webview by clicking on react native button 如何在React Native中无效的登录凭据时显示警告消息? - How to show alert message when invalid login credentials in React Native? 如果输入框的内容已更改且未按下按钮,如何显示警报消息? - How can I show alert message if input box content is changed and button is not pressed? 单击按钮时如何退出反应本机应用程序? - how to exit react native app when clicking a button? React Native:为什么在点击按钮之前会显示警报? - React Native : Why alert is getting displayed before clicking on the button? 如何在使用 react 和 typescript 单击按钮时在应用程序中的任何位置显示对话框? - How to show the dialog everywhere in the app on clicking a button using react and typescript? 单击后,单选按钮和复选框不显示警报消息,您看我是否出错? - The radio buttons and checkbox do not show the alert message after clicking, Can you look if I made a mistake?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM