The*_*com 17 alert android ios web react-native
我刚刚开始学习和练习 React Native,我遇到了第一个问题,我似乎可以自己解决。
\n我有以下代码,非常简单,但是当我在网络上运行它时,Alert.alert() 不起作用。如果我单击该按钮,则不会发生任何事情,但是,当我在 iOS 或 Android 模拟器上单击该按钮时,它工作正常。
\nimport { StatusBar } from 'expo-status-bar';\nimport React from 'react';\nimport { StyleSheet, Text, Button, View, Alert } from 'react-native';\n\nexport default function App() {\n return (\n <View style={styles.container}>\n <Text style={styles.headerStyle} >Practice App</Text>\n <Text style={{padding: 10}}>Open up App.js to start working on your app!</Text>\n <Button\n onPress={() => alert('Hello, Nice To Meet You :)')}\n title="Greet Me"\n />\n <StatusBar style="auto" />\n </View>\n );\n}\nRun Code Online (Sandbox Code Playgroud)\n我也知道alert()适用于所有三种设备,但是,我想了解为什么Alert.alert()仅适用于iOS和Android。
\n我的问题更多的是为了理解而不是寻找解决方案。是使用alert()的唯一解决方案,还是我以错误的方式实现Alert.alert()?
\nMeh*_*lan 17
此解决方法基本上模仿了浏览器方法react-native的行为:Alertwindow.confirm
# alert.js
import { Alert, Platform } from 'react-native'
const alertPolyfill = (title, description, options, extra) => {
const result = window.confirm([title, description].filter(Boolean).join('\n'))
if (result) {
const confirmOption = options.find(({ style }) => style !== 'cancel')
confirmOption && confirmOption.onPress()
} else {
const cancelOption = options.find(({ style }) => style === 'cancel')
cancelOption && cancelOption.onPress()
}
}
const alert = Platform.OS === 'web' ? alertPolyfill : Alert.alert
export default alert
Run Code Online (Sandbox Code Playgroud)
用法:
前:
import { Alert } from 'react-native'
Alert.alert(...)
Run Code Online (Sandbox Code Playgroud)
后:
import alert from './alert'
alert(...)
Run Code Online (Sandbox Code Playgroud)
来源和学分:https://github.com/necolas/react-native-web/issues/1026#issuecomment-679102691
小智 3
React Native 是一个适用于 Android、iOS 和 Web 的开源移动应用程序框架,但没有适用于 Web 的警报组件,但我找到了一个可以为您提供解决方案的包。这就是安装包了
npm i react-native-awesome-alerts
Run Code Online (Sandbox Code Playgroud)
这个例子会对你有帮助
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,
},
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9190 次 |
| 最近记录: |