Correctly memoize the promise from a function depending on deps when using useAsync.
This commit is contained in:
		
							parent
							
								
									e2fc741fee
								
							
						
					
					
						commit
						1963b6208a
					
				
					 2 changed files with 10 additions and 6 deletions
				
			
		|  | @ -1,5 +1,5 @@ | |||
| { | ||||
| 	"version": "1.4.0", | ||||
| 	"version": "1.4.1", | ||||
| 	"name": "@kernelui/core", | ||||
| 	"description": "Kernel UI Core.", | ||||
| 	"scripts": { | ||||
|  |  | |||
|  | @ -1,4 +1,4 @@ | |||
| import React, {useEffect, useState} from "react"; | ||||
| import React, {useEffect, useMemo, useState} from "react"; | ||||
| 
 | ||||
| /** | ||||
|  * A type that can be returned by a promise or as is. | ||||
|  | @ -41,10 +41,14 @@ export type PromiseFn<T> = () => Promise<T>; | |||
| export function useAsync<T>(promise: Promisable<T>|PromiseFn<T>, deps: any[] = []): [AsyncState<T>, React.Dispatch<T>] | ||||
| { | ||||
| 	// Get the actual promise from the function if there is one.
 | ||||
| 	promise = useMemo(() => { | ||||
| 		if ((promise as PromiseFn<T>)?.call) | ||||
| 		promise = (promise as PromiseFn<T>)(); | ||||
| 			return (promise as PromiseFn<T>)(); | ||||
| 		else if (promise instanceof Promise) | ||||
| 		promise = Promise.race([promise as Promise<T>]); | ||||
| 			return Promise.race([promise as Promise<T>]); | ||||
| 		else | ||||
| 			return promise; | ||||
| 	}, deps); | ||||
| 
 | ||||
| 	// The async state.
 | ||||
| 	const [state, setState] = useState<AsyncState<T>>({ | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue