mirror of
https://github.com/streamich/react-use.git
synced 2025-12-08 18:02:14 +00:00
25 lines
534 B
Markdown
25 lines
534 B
Markdown
# `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);
|
|
})();
|
|
});
|
|
};
|
|
```
|