# react-modal-util

> react-modal-util

Latest version **1.0.0-alpha.1** (published 2022-11-08) · 0 weekly downloads

## Install

```sh
npm install react-modal-util
pnpm add react-modal-util
yarn add react-modal-util
bun add react-modal-util
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0-alpha.1 |
| Published | 2022-11-08 |
| First published | 2022-11-08 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 337.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | seauning |

## Links

- npm: https://www.npmjs.com/package/react-modal-util
- npm.io page: https://npm.io/package/react-modal-util

## Recent versions

- 1.0.0-alpha.1 (latest) — 2022-11-08

## README

# 背景

> 过去要使用一个 Modal 组件需要以下步骤
>
> 1. 引入 Modal
> 2. 视图中使用 Modal
> 3. 定义显隐状态
>
> 而 `3.` 是重复性的代码，我们能否将这种重复性的代码给封装起来简化受控

# 功能点

1. invokeModal

```tsx
import { Modal, Button } from 'antd';
import { invokeModal } from 'react-modal-utils';
function Demo() {
  const modal = invokeModal(Modal, {
    onCancel: () => {
      modal.close();
    },
  });
  return <Button onClick={() => modal.open()}>打开</Button>;
}
```

2. useModal Hook

```tsx
import { Modal, Button } from 'antd';
import { useModal } from 'react-modal-utils';
function Demo() {
  const [modal, placeholder] = useModal(Modal, {
    onCancel: () => {
      modal.close();
    },
  });
  return (
    <>
      <Button onClick={() => modal.open()}>打开</Button>
      {placeholder}
    </>
  );
}
```

# 工具库使用前后对比

1. invokeModal

![](./src/static/imgs/compare1.png)

2. useModal

![](./src/static/imgs/compare2.png)

![](./src/static/imgs/context.png)

---
_Source: https://npm.io/package/react-modal-util · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
