-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# `usePromise` | ||
|
||
React Lifecycle hook that returns a helper function for wrapping promises. | ||
Promises wrapped with this function will resolve only when component is mounted. | ||
|
||
|
||
## Usage | ||
|
||
```jsx | ||
import {usePromise} from 'react-use'; | ||
|
||
const Demo = ({promise}) => { | ||
const mounted = usePromise(); | ||
const [value, setValue] = useState(); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
const value = await mounted(promise); | ||
// This line will not execute if <Demo> component gets unmounted. | ||
setValue(value); | ||
})(); | ||
}); | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import {storiesOf} from '@storybook/react'; | ||
import * as React from 'react'; | ||
import {usePromise, useBoolean, useNumber} from '..'; | ||
import ShowDocs from '../util/ShowDocs'; | ||
|
||
const {useState, useEffect} = React; | ||
|
||
const DemoInner = ({promise}) => { | ||
const safePromise = usePromise(); | ||
const [value, setValue] = useState<number>(-1); | ||
useEffect(() => { | ||
safePromise(promise).then(setValue); | ||
}, [promise]); | ||
|
||
return ( | ||
<div> | ||
{value === -1 ? 'Resolving value...' : 'Value: ' + value} | ||
</div> | ||
); | ||
}; | ||
|
||
const Demo = () => { | ||
const [mounted, toggleMounted] = useBoolean(true); | ||
const [num, {inc}] = useNumber(); | ||
const promise = new Promise(r => setTimeout(() => r(num), 1_000)); | ||
|
||
return ( | ||
<div> | ||
<p>This demo provides a number in a promise that resolves in 1sec to a child component.</p> | ||
<button onClick={() => toggleMounted()}>{mounted ? 'Unmount' : 'Mount'}</button> | ||
<button onClick={() => inc()}>Increment ({num})</button> | ||
<br /> | ||
{mounted && <DemoInner promise={promise} />} | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf('Lifecycles|usePromise', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/usePromise.md')} />) | ||
.add('Demo', () => | ||
<Demo/> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {useCallback} from 'react'; | ||
import useRefMounted from './useRefMounted'; | ||
|
||
export type UsePromise = () => <T>(promise: Promise<T>) => Promise<T>; | ||
|
||
const usePromise: UsePromise = () => { | ||
const refMounted = useRefMounted(); | ||
return useCallback(<T>(promise: Promise<T>): Promise<T> => | ||
new Promise<T>((resolve, reject) => { | ||
promise.then(value => { | ||
if (refMounted.current) { | ||
resolve(value); | ||
} | ||
}, error => { | ||
if (refMounted.current) { | ||
reject(error); | ||
} | ||
}) | ||
}), []); | ||
}; | ||
|
||
export default usePromise; |