Back to Refine

useModal Hook | Ant Design Modal Management

documentation/versioned_docs/version-4.xx.xx/ui-integrations/ant-design/hooks/use-modal/index.md

3.25.01.5 KB
Original Source

The useModal hook helps you manage the Ant Design Modal component.

ts
const { show, close, modalProps } = useModal();

You can use the show and close props to control the modal visibility. You have to descturt modalProps to the <Modal/> component.

Usage

Let's see an example:

tsx
// highlight-start
import { useModal } from "@refinedev/antd";
import { Modal, Button } from "antd";
// highlight-end

export const PostList: React.FC = () => {
  // highlight-next-line
  const { show, modalProps } = useModal();

  return (
    <>
      // highlight-start
      <Button onClick={show}>Show Modal</Button>
      <Modal {...modalProps}>
        <p>Modal Content</p>
      </Modal>
      // highlight-end
    </>
  );
};

Here, we show a button somewhere on the page and use show on it's onClick callback to trigger opening of the <Modal>. When the user clicks on the button, the <Modal> appears.

API Reference

Properties

<PropsTable module="@refinedev/antd/useModal" />

Return Value

KeyDescriptionType
showReturns the visibility state of the Modal() => void
closeA function that can open the modal() => void
modalPropsSpecify a function that can close the modal() => void