https://github.com/christoph-jerolimov/usepromises
React and React Native hook to consume a Promise (similar to useEffect) with full TypeScript support
https://github.com/christoph-jerolimov/usepromises
react react-hooks react-native reactjs reactnative typescript
Last synced: 6 months ago
JSON representation
React and React Native hook to consume a Promise (similar to useEffect) with full TypeScript support
- Host: GitHub
- URL: https://github.com/christoph-jerolimov/usepromises
- Owner: christoph-jerolimov
- License: mit
- Created: 2020-03-09T23:40:59.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-07-23T12:41:35.000Z (almost 2 years ago)
- Last Synced: 2025-03-28T14:15:50.466Z (over 1 year ago)
- Topics: react, react-hooks, react-native, reactjs, reactnative, typescript
- Language: TypeScript
- Homepage:
- Size: 994 KB
- Stars: 2
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# usePromises
**React and React Native hook to consume a Promise (similar to useEffect) with full TypeScript support**
## Installation
```
npm install --save usepromises
```
or
```
yarn add usepromises
```
## Usage / Examples
### usePromise
```javascript
import { usePromise } from 'usepromises';
...
interface SampleResponse {
slideshow: {
title: string
}
}
function Example() {
const response = usePromise(async () => {
const response = await fetch('https://httpbin.org/json');
return response.json();
}, []);
return (
{response.isResolved && response.value.slideshow.title}
{response.isRejected ? `Error: ${response.error}` : null}
);
};
```
### useMountPromise
```javascript
import { useMountPromise } from 'usepromises';
...
interface SampleData {
slideshow: {
title: string
}
}
function Example() {
const [data, setData] = useState();
useMountPromise(async () => {
const response = await fetch('https://httpbin.org/json');
setData(await response.json());
});
...
};
```
### useUnmountPromise
```javascript
import { useUnmountPromise } from 'usepromises';
...
interface SampleResponse {
slideshow: {
title: string
}
}
function Example() {
useUnmountPromise(async () => {
const response = await fetch('https://httpbin.org/json');
console.warn('Server status code:', response.status);
});
...
};
```