如何解决React Native Alert.alert() 仅适用于 iOS 和 Android 而不是 Web
我刚刚开始学习和实践 React Native,遇到了第一个问题,我自己似乎无法解决。
我有以下代码,非常简单,但是当我在网络上运行 Alert.alert() 时它不起作用。如果我点击按钮没有任何反应,但是,当我点击 iOS 或 Android 模拟器上的按钮时,它工作正常。
solver=SoverFactory('glpk') # or other legal solver name that you have installed
results = solver.solve(model)
print(results)
model.display()
我也知道 alert() 适用于所有三种设备,但是,我想了解为什么 Alert.alert() 仅适用于 iOS 和 Android。
我的问题更多是为了理解而不是寻找解决方案。是使用 alert() 的唯一解决方案,还是我以错误的方式实施 Alert.alert()?
解决方法
React Native 是适用于 Android、iOS 和 Web 的开源移动应用程序框架,但没有适用于 Web 的警报组件,但我找到了一个可以为您提供解决方案的软件包。就是安装包
npm i react-native-awesome-alerts
这个例子会帮助你
import React from "react";
import { StyleSheet,Text,View,TouchableOpacity } from "react-native";
import Alert from "react-native-awesome-alerts";
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = { showAlert: false };
}
showAlert = () => {
this.setState({
showAlert: true,});
};
hideAlert = () => {
this.setState({
showAlert: false,});
};
render() {
const { showAlert } = this.state;
return (
<View style={styles.container}>
<Text>Practice App</Text>
<Text style={{ padding: 10 }}>
Open up App.js to start working on your app!
</Text>
<TouchableOpacity
onPress={() => {
this.showAlert();
}}
>
<View style={styles.button}>
<Text style={styles.text}>Greet Me</Text>
</View>
</TouchableOpacity>
<Alert
show={showAlert}
message="Hello,Nice To Meet You :"
closeOnTouchOutside={true}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,alignItems: "center",justifyContent: "center",backgroundColor: "#fff",},button: {
margin: 10,paddingHorizontal: 10,paddingVertical: 7,borderRadius: 5,backgroundColor: "#AEDEF4",text: {
color: "#fff",fontSize: 15,});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。