I have a following function which I use to control my modals:
const getModalProps = useCallback(
(id) => ({
onClose: () => handleClose(id),
onOpen: () => handleOpen(id),
isOpen: isOpen(id),
}),
[handleClose, handleOpen, isOpen],
);
The idea is to be used something like <Modal {...getModalProps('userModal')} />. Problem arrives when I want to use it in the following way:
const modalProps = getModalProps('userModal');
useEffect(() => { modalProps.onOpen() }, [modalProps.onOpen])
This causes an infinite loop, because modalProps.onOpen is actually an anonymous arrow function, instead of useCallback memoised function.
I am at loss of ideas as to how I can use getModalProps function which returns memoised, but not invoked functions of handleOpen and handleClose.
Something like this is the idea (I am aware this cannot work, just a concept):
const getModalProps = useCallback(
(id) => ({
onClose: memoise(() => handleClose(id)),
onOpen: memoise(() => handleOpen(id)),
isOpen: isOpen(id),
}),
[handleClose, handleOpen, isOpen],
);
handleClose and handleOpen are just your normal useCallback functions.
Please leave a comment if more info is needed.