Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use two modals on same page using useDisclosure() in ChakraUI

There is a custom hook useDisclosure() provided by chakraUI which returns isOpen, onClose , onOpen.

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

The onOpen is passed to the onClick of the button which is triggered to open the modal.

<Modal isOpen={isOpen} onClose={onClose}>
  ...Modal Code...
<Modal/>

<Button onClick={onOpen}>
  button
<Button/>

Now I want to make another modal (lets say reportModal) on same page. For that I wrote the same code where I renamed variables while destructuring useDisclosure().

const {
        isOpen: { isOpenReportModal },
        onOpen: { onOpenReportModal },
        onClose: { onCloseReportModal },
      } = useDisclosure() 

Further, I used the same flow by passing these renamed variables to and component but id didn't work.

Anyone for its solution? Thanking in advance...

like image 905
Ibad Shaikh Avatar asked Jan 22 '21 08:01

Ibad Shaikh


People also ask

How do you use multiple modals in Chakra UI?

function MyComponent() { const modal1 = useDisclosure() const modal2 = useDisclosure() return ( <> <Button onClick={modal1. onOpen}>Open Modal 1</Button> <Button onClick={modal2. onOpen}>Open Modal 2</Button> <Modal isOpen={modal1. isOpen} onClose={modal1.

What is useDisclosure in react?

useDisclosure is a custom hook used to help handle common open , close , or toggle scenarios. It can be used to control feedback component such as Modal, AlertDialog, Drawer, etc.

What is Modal in chakra?

Modal : The wrapper that provides context for its children. ModalOverlay : The dimmed overlay behind the modal dialog. ModalContent : The container for the modal dialog's content. ModalHeader : The header that labels the modal dialog.


1 Answers

You have to rename the variables like this.

 const { 
    isOpen: isOpenReportModal, 
    onOpen: onOpenReportModal, 
    onClose: onCloseReportModal 
} = useDisclosure()

Now this should work. What you have tried is like destructuring again. Which is wrong.

like image 77
Praveenkumar Avatar answered Oct 24 '22 00:10

Praveenkumar