usePromise()
ts
function usePromise<T>(inputPromise): T | null;Defined in: promise.ts:37
React hook for handling promises. It returns the resolved value of the promise or null if the promise is still pending or has been rejected. The hook also ensures that state updates only occur if the component is still mounted, preventing potential memory leaks or errors from trying to update an unmounted component.
Type Parameters
| Type Parameter | Description |
|---|---|
T | Type of the resolved value of the promise. |
Parameters
| Parameter | Type |
|---|---|
inputPromise | Promise<T> |
Returns
T | null
The resolved value of the promise of type T or null if the promise is still pending or has been rejected.
Example
tsx
function MyComponent () {
const data = usePromise(fetch(
"https://jsonplaceholder.typicode.com/todos/1",
{
method: "GET"
}
));
return (
<>
{
data
? <p>Data: {JSON.stringify(data)}</p>
: <p>Loading...</p>
}
</>
)
}Since
1.0.0
Author
Simon Kovtyk