Skip to content

Modal API feels wired #2077

Answered by ljosberinn
ivanjeremic asked this question in General
Sep 22, 2020 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

You may wrap onClose in your own function and forward that:

const { isOpen, onOpen, onClose } = useDisclosure();

function customOnClose() {
  // do something here
  onClose();
}

return <Modal onClose={customOnClose}>...

FWIW, you don't need to use useDisclosure necessarily. You can reinvent the entire thing with your own states instead, but it usually leads to more code:

const [isOpen, setIsOpen] = useState(false);

function open() {
  // custom logic here
  setIsOpen(true);
}

function close() {
  // custom logic here
  setIsOpen(false);
}

return (
  <>
    <button type="button" onClick={open}>toggle</button>
    <Modal isOpen={isOpen} onClose={close}>...</Modal>
  </>
)

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@ivanjeremic
Comment options

@ljosberinn
Comment options

Answer selected by ljosberinn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants