-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuseAlert.js
55 lines (43 loc) · 1.32 KB
/
useAlert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { useState } from 'react';
const useAlert = () => {
const [isAlertVisible, setIsAlertVisible] = useState(false);
const handleOpenAlert = () => setIsAlertVisible(true);
const handleConfirm = () => setIsAlertVisible(false);
const handleCancel = () => setIsAlertVisible(false);
return {
isAlertVisible,
handleOpenAlert,
handleConfirm,
handleCancel,
};
};
export default useAlert;
--------------------------------------------------------------------
import React, { useState } from 'react';
import { View, Button, StyleSheet } from 'react-native';
import AlertComponent from './AlertComponent'; // Make sure the path is correct
import useAlert from './hooks/useAlert';
const HomeScreen = () => {
const { isAlertVisible, handleOpenAlert, handleConfirm, handleCancel } = useAlert();
return (
<View style={styles.container}>
<Button title="Show Alert" onPress={handleOpenAlert} />
<AlertComponent
visible={isAlertVisible}
title="User not found"
onCancel={handleCancel}
message="You have to use a different name"
onConfirm={handleConfirm}
confirmText="Ok"
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
});
export default HomeScreen;