How to don't close ModalContainer if "areYouSureModal" isOpen?

// If you know how to do it in better way - do it
const { isLoading } = useLoading()
const isAreYouSureModalOpen = useAreYouSureModalsStore.getState().isAreYouSureModalOpen

//correct way to add event listener to listen keydown
useEffect(() => {
document.addEventListener("keyup", handleKeyDown)
return () => document.removeEventListener("keyup", handleKeyDown)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isLoading])

/* onClose - close modal - show scrollbar */
function closeModal() {
onClose()
}

console.log(49, "isAreYouSureModalOpen - ", isAreYouSureModalOpen)
// Close modal on esc
const handleKeyDown = useCallback(
(event: KeyboardEvent) => {
if (event.key === "k" && (event.metaKey || event.ctrlKey)) {
//to prevent focus state on browser searchbar
// TODO - import ctrlK modal here and if user press ctrl+K close current modal and open ctrlK modal
event.preventDefault()
}

if (event.key === "Escape" && !isLoading && !isAreYouSureModalOpen) {
console.log(61, "are you sure modal closed - close ModalContainer")
closeModal()
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[isAreYouSureModalOpen, isLoading],
)
// If you know how to do it in better way - do it
const { isLoading } = useLoading()
const isAreYouSureModalOpen = useAreYouSureModalsStore.getState().isAreYouSureModalOpen

//correct way to add event listener to listen keydown
useEffect(() => {
document.addEventListener("keyup", handleKeyDown)
return () => document.removeEventListener("keyup", handleKeyDown)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isLoading])

/* onClose - close modal - show scrollbar */
function closeModal() {
onClose()
}

console.log(49, "isAreYouSureModalOpen - ", isAreYouSureModalOpen)
// Close modal on esc
const handleKeyDown = useCallback(
(event: KeyboardEvent) => {
if (event.key === "k" && (event.metaKey || event.ctrlKey)) {
//to prevent focus state on browser searchbar
// TODO - import ctrlK modal here and if user press ctrl+K close current modal and open ctrlK modal
event.preventDefault()
}

if (event.key === "Escape" && !isLoading && !isAreYouSureModalOpen) {
console.log(61, "are you sure modal closed - close ModalContainer")
closeModal()
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[isAreYouSureModalOpen, isLoading],
)
49 console log throw actual state (true) 61 console log throw not actual state (false) How to console log actual state in 61 console log (to don't close modal if are you sure modal isOpen)
1 Reply
Nikita
Nikita5mo ago
I put state inside handleKeyDown function
const isAreYouSureModalOpen = useAreYouSureModalsStore.getState().isAreYouSureModalOpen
const isAreYouSureModalOpen = useAreYouSureModalsStore.getState().isAreYouSureModalOpen