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

https://github.com/jonbnewman/use-prompt

Display a react component to a user asynchronously
https://github.com/jonbnewman/use-prompt

async async-await hook hooks react typescript

Last synced: 15 days ago
JSON representation

Display a react component to a user asynchronously

Awesome Lists containing this project

README

        

# use-prompt

![CI](https://github.com/jonbnewman/use-prompt/actions/workflows/main.yml/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/jonbnewman/use-prompt/badge.svg?branch=main&r=1)](https://coveralls.io/github/jonbnewman/use-prompt?branch=main)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/d3122dfe36f8442894cfc239be96d056)](https://www.codacy.com/gh/jonbnewman/use-prompt/dashboard?utm_source=github.com&utm_medium=referral&utm_content=jonbnewman/use-prompt&utm_campaign=Badge_Grade)

[![NPM Package](https://img.shields.io/npm/v/use-prompt.svg?logo=npm)](https://www.npmjs.com/package/use-prompt)
![Typescript](https://img.shields.io/badge/types-TypeScript-blue?logo=typescript)
![Package size](https://img.shields.io/bundlephobia/minzip/use-prompt)
![MIT License](https://img.shields.io/github/license/jonbnewman/use-prompt)

**use-prompt** is a [React Hook](https://reactjs.org/docs/hooks-intro.html) that lets you conveniently display a component to a user asynchronously.

This allows you to ask a user for input, prompt for an answer, display a message, or do whatever you want in an asynchronous manner.

Features:

- Use your own custom components
- Promise-based (async/await and try/catch capable)
- N-number of concurrent prompt support
- Render anywhere you like for each prompt
- Typescript support
- Minimalistic, easy to use API
- Very small bundle size

---

1. [API Details](https://useprompt.jonbnewman.dev/api)
1. [Typescript](https://useprompt.jonbnewman.dev/typescript)
1. [Examples](https://useprompt.jonbnewman.dev/examples)
- [Basic example](https://useprompt.jonbnewman.dev/examples/basic-example)
- [Async/await and try/catch](https://useprompt.jonbnewman.dev/examples/async)
- [Sequential prompts](https://useprompt.jonbnewman.dev/examples/sequential)
- [Concurrent prompts](https://useprompt.jonbnewman.dev/examples/concurrent)
- [Persistant prompts](https://useprompt.jonbnewman.dev/examples/persistant)
- [Render control](https://useprompt.jonbnewman.dev/examples/render-control)

---

## Installation

```bash
npm i use-prompt
```

```bash
yarn add use-prompt
```

## API

```javascript
const [prompt, showPrompt, isVisible, clearPrompt] = usePrompt(
options?: {
persist?: boolean;
}
): [RenderedPrompt, ShowPrompt, IsVisible, ClearPrompt]
```

See the [API Details page](https://useprompt.jonbnewman.dev/api) for more information.

## Basic example

The following demonstrates a very basic example use of `usePrompt`:

```javascript
import usePrompt from 'use-prompt';

function App() {
const [prompt, showPrompt, isVisible] = usePrompt();

function showMyPrompt() {
showPrompt(({ resolve }) => (
<>

She sells seashells by the seashore.

Ok thanks
>
));
}

return (



Show prompt

{prompt}

);
}
```

### Async/await and try/catch

Use async/await and try/catch in order to retrieve what the users response within the prompt was:

```javascript
import { useState } from 'react';
import usePrompt from 'use-prompt';

function App() {
const [prompt, showPrompt, isVisible] = usePrompt();
const [cancelReason, setCancelReason] = useState(null);
const [confirmResponse, setConfirmResponse] = useState(null);

async function showMyPrompt() {
try {
const response = await showPrompt(({ resolve, reject }) => (
<>

Are you sure?

reject('clicked cancel')}>Cancel
resolve('clicked yes')}>Yes
>
));
setConfirmResponse(response);
} catch (reason) {
setCancelReason(reason);
}
}

return (
<>

Show prompt

{prompt}
{confirmResponse && `Prompt was confirmed: ${confirmResponse}`}
{cancelReason && `Prompt was cancelled: ${cancelReason}`}
>
);
}
```

## Sequential prompts

Display prompts one after another in a sequential manner:

```javascript
import usePrompt from 'use-prompt';

function Prompt({ resolve, message }) {
return (
<>

{message}

Ok thanks
>
);
}

function App() {
const [prompt, showPrompt, isVisible] = usePrompt();

async function showPrompts() {
await showPrompt((props) => );
await showPrompt((props) => );
await showPrompt((props) => );
}

return (



Show prompts

{prompt}

);
}
```

## Concurrent prompts

Display as many prompts concurrently as you want:

```javascript
import usePrompt from 'use-prompt';

function Prompt({ resolve, message }) {
return (
<>

{message}

Ok thanks
>
);
}

function App() {
const [prompt1, showPrompt1, isVisible1] = usePrompt();
const [prompt2, showPrompt2, isVisible2] = usePrompt();
const [prompt3, showPrompt3, isVisible3] = usePrompt();

function triggerPrompt1() {
showPrompt1((props) => );
}
function triggerPrompt2() {
showPrompt2((props) => );
}
function triggerPrompt3() {
showPrompt3((props) => );
}

return (



Show prompt 1

{prompt1}


Show prompt 2

{prompt2}


Show prompt 3

{prompt3}


);
}
```

---

**[See the full docs](https://useprompt.jonbnewman.dev)**