Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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: about 1 month ago
JSON representation

React and React Native hook to consume a Promise (similar to useEffect) with full TypeScript support

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);
});

...
};
```