react-modal-hook icon indicating copy to clipboard operation
react-modal-hook copied to clipboard

Modal inside modal: closing the first modal closes the second

Open vicjune opened this issue 3 years ago • 1 comments

const FirstModal = ({ closeFirstModal }) => {
  const [openSecondModal] = useModal(() => <div>Second modal</div>);

  return (
    <div
      onClick={() => {
        openSecondModal();

        // This closes the second modal as well but it should not
        closeFirstModal();
      }}
    >
      First modal
    </div>
  );
};

const Page = () => {
  const [openFirstModal, closeFirstModal] = useModal(() => (
    <FirstModal closeFirstModal={closeFirstModal} />
  ));

  return <div onClick={openFirstModal}>Click me</div>;
};

In this situation, you click on the div Click me -> it displays First modal

Then when you click on the div First modal:

Expected behaviour: It should display Second modal and close FirstModal

What happens: It closes FirstModal and displays Second modal for a fraction of a second before closing it

vicjune avatar Feb 09 '22 16:02 vicjune

Was looking for a "fire and leave" pattern, I have a menu item triggering a modal. This runs into the same problem here, the hook will cleanup, ie close the modal, when the parent component unmounts: https://github.com/mpontus/react-modal-hook/blob/master/src/useModal.ts#L64

Perhaps an optional flag could be added to disable the clean up?

zbyte64 avatar Aug 30 '22 21:08 zbyte64