Merge pull request #5759 from BlueWallet/rerenders

FIX: Eliminate re-renders
This commit is contained in:
GLaDOS 2023-10-22 14:42:05 +01:00 committed by GitHub
commit 1ac68c5c84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 5 deletions

View File

@ -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 };
}

View File

@ -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();