diff --git a/class/useAsyncPromise.ts b/class/useAsyncPromise.ts new file mode 100644 index 000000000..2dc3c79f4 --- /dev/null +++ b/class/useAsyncPromise.ts @@ -0,0 +1,40 @@ +import { useState, useEffect } from 'react'; + +/** + * A custom React hook that accepts a promise and returns the resolved value and any errors that occur. + * + * @template T - The type of the resolved value. + * @param {Promise} promise - The promise to be resolved. + * @returns {{ data: T | null, error: Error | null, loading: boolean }} - An object with the resolved data, any error, and loading state. + */ +function useAsyncPromise(promise: Promise): { data: T | null; error: Error | null; loading: boolean } { + const [data, setData] = useState(null); + const [error, setError] = useState(null); + const [loading, setLoading] = useState(true); + + useEffect(() => { + let isMounted = true; + + promise + .then(result => { + if (isMounted) { + setData(result); + setLoading(false); + } + }) + .catch(err => { + if (isMounted) { + setError(err); + setLoading(false); + } + }); + + return () => { + isMounted = false; + }; + }, [promise]); + + return { data, error, loading }; +} + +export default useAsyncPromise; diff --git a/screen/settings/about.js b/screen/settings/about.js index 8b088bb21..396363e6d 100644 --- a/screen/settings/about.js +++ b/screen/settings/about.js @@ -1,8 +1,8 @@ -import React, { useContext, useEffect, useState } from 'react'; +import React, { useContext } from 'react'; import { TouchableOpacity, ScrollView, Linking, Image, View, Text, StyleSheet, useWindowDimensions, Platform, Alert } from 'react-native'; import { useNavigation, useTheme } from '@react-navigation/native'; import { Icon } from 'react-native-elements'; -import { getApplicationName, getVersion, getBundleId, getBuildNumber, getUniqueId, hasGmsSync } from 'react-native-device-info'; +import { getApplicationName, getVersion, getBundleId, getBuildNumber, getUniqueIdSync, hasGmsSync } from 'react-native-device-info'; import Rate, { AndroidMarket } from 'react-native-rate'; import { BlueButton, BlueCard, BlueListItem, BlueSpacing20, BlueTextCentered } from '../../BlueComponents'; import navigationStyle from '../../components/navigationStyle'; @@ -20,7 +20,6 @@ const About = () => { const { colors } = useTheme(); const { width, height } = useWindowDimensions(); const { isElectrumDisabled } = useContext(BlueStorageContext); - const [uniqueID, setUniqueID] = useState(); const styles = StyleSheet.create({ copyToClipboard: { justifyContent: 'center', @@ -76,10 +75,6 @@ const About = () => { }, }); - useEffect(() => { - getUniqueId().then(setUniqueID); - }, []); - const handleOnReleaseNotesPress = () => { navigate('ReleaseNotes'); }; @@ -249,12 +244,12 @@ const About = () => { w, h = {width}, {height} - Unique ID: {uniqueID} + Unique ID: {getUniqueIdSync()} { - const stringToCopy = 'userId:' + uniqueID; + const stringToCopy = 'userId:' + getUniqueIdSync(); A.logError('copied unique id'); Clipboard.setString(stringToCopy); }}