mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2025-01-19 05:45:15 +01:00
FIX: Eliminate re-renders
This commit is contained in:
parent
0a2c700781
commit
1fff1f44b5
@ -4,10 +4,10 @@ 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<T>} promise - The promise to be resolved.
|
||||
* @param {() => Promise<T>} promiseFn - A function that returns 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<T>(promise: Promise<T>): { data: T | null; error: Error | null; loading: boolean } {
|
||||
function useAsyncPromise<T>(promiseFn: () => Promise<T>) {
|
||||
const [data, setData] = useState<T | null>(null);
|
||||
const [error, setError] = useState<Error | null>(null);
|
||||
const [loading, setLoading] = useState<boolean>(true);
|
||||
@ -15,7 +15,7 @@ function useAsyncPromise<T>(promise: Promise<T>): { data: T | null; error: Error
|
||||
useEffect(() => {
|
||||
let isMounted = true;
|
||||
|
||||
promise
|
||||
promiseFn()
|
||||
.then(result => {
|
||||
if (isMounted) {
|
||||
setData(result);
|
||||
@ -32,7 +32,7 @@ function useAsyncPromise<T>(promise: Promise<T>): { data: T | null; error: Error
|
||||
return () => {
|
||||
isMounted = false;
|
||||
};
|
||||
}, [promise]);
|
||||
}, [promiseFn]);
|
||||
|
||||
return { data, error, loading };
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ const WalletsAdd = () => {
|
||||
const [walletBaseURI, setWalletBaseURI] = useState('');
|
||||
const [selectedIndex, setSelectedIndex] = useState(0);
|
||||
const [label, setLabel] = useState('');
|
||||
const isAdvancedOptionsEnabled = useAsyncPromise(isAdvancedModeEnabled());
|
||||
const isAdvancedOptionsEnabled = useAsyncPromise(isAdvancedModeEnabled);
|
||||
const [selectedWalletType, setSelectedWalletType] = useState(false);
|
||||
const [backdoorPressed, setBackdoorPressed] = useState(1);
|
||||
const { navigate, goBack, setOptions } = useNavigation();
|
||||
|
Loading…
Reference in New Issue
Block a user