Skip to content

Commit 6fcd491

Browse files
committed
Handle alerts correctly on web in Button example
1 parent 9d5e577 commit 6fcd491

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

website/versioned_docs/version-0.82/button.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,20 @@ If this button doesn't look right for your app, you can build your own button us
2020

2121
```SnackPlayer name=Button%20Example
2222
import React from 'react';
23-
import {StyleSheet, Button, View, Text, Alert} from 'react-native';
23+
import {StyleSheet, Button, View, Text, Alert, Platform} from 'react-native';
2424
import {SafeAreaView, SafeAreaProvider} from 'react-native-safe-area-context';
2525
2626
const Separator = () => <View style={styles.separator} />;
2727
28+
function showAlert(message: string){
29+
if(Platform.OS === "web"){
30+
window.alert(message);
31+
}
32+
else{
33+
Alert.alert(message)
34+
}
35+
}
36+
2837
const App = () => (
2938
<SafeAreaProvider>
3039
<SafeAreaView style={styles.container}>
@@ -35,7 +44,7 @@ const App = () => (
3544
</Text>
3645
<Button
3746
title="Press me"
38-
onPress={() => Alert.alert('Simple Button pressed')}
47+
onPress={() => showAlert('Simple Button pressed')}
3948
/>
4049
</View>
4150
<Separator />
@@ -48,7 +57,7 @@ const App = () => (
4857
<Button
4958
title="Press me"
5059
color="#f194ff"
51-
onPress={() => Alert.alert('Button with adjusted color pressed')}
60+
onPress={() => showAlert('Button with adjusted color pressed')}
5261
/>
5362
</View>
5463
<Separator />
@@ -59,7 +68,7 @@ const App = () => (
5968
<Button
6069
title="Press me"
6170
disabled
62-
onPress={() => Alert.alert('Cannot press this one')}
71+
onPress={() => showAlert('Cannot press this one')}
6372
/>
6473
</View>
6574
<Separator />
@@ -70,11 +79,11 @@ const App = () => (
7079
<View style={styles.fixToText}>
7180
<Button
7281
title="Left button"
73-
onPress={() => Alert.alert('Left button pressed')}
82+
onPress={() => showAlert('Left button pressed')}
7483
/>
7584
<Button
7685
title="Right button"
77-
onPress={() => Alert.alert('Right button pressed')}
86+
onPress={() => showAlert('Right button pressed')}
7887
/>
7988
</View>
8089
</View>

0 commit comments

Comments
 (0)