Skip to content

Instantly share code, notes, and snippets.

@sairajchouhan
Created July 13, 2022 07:10
Show Gist options
  • Save sairajchouhan/374a73d0b9f43b1bb8f5bc78a6f7b14a to your computer and use it in GitHub Desktop.
Save sairajchouhan/374a73d0b9f43b1bb8f5bc78a6f7b14a to your computer and use it in GitHub Desktop.
import { useState } from "react"
const useModal = (initial = false, duration = 300) => {
const [isOpen, setIsOpen] = useState(initial)
const closeModal = () => {
return new Promise((res) => {
setIsOpen(false)
setTimeout(() => {
res(true)
}, duration)
})
}
const openModal = () => {
setIsOpen(true)
}
return {
isOpen,
closeModal,
openModal,
}
}
export default useModal
@sairajchouhan
Copy link
Author

This can be used to close the form and then reset the state of the contents inside of the modal, I got this issue when using @headlessui/react where the modal closes on transition for 300ms but the state was getting reset before the modal gets closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment