interface ModalType {
component: JSX.Element;
props: object;
}
function Modal({ title, message, onConfirm }: ModalProps) {
const [modal, setModal] = useState<ModalType|{}>({});
// useEffect(()=> {
// ModalServcie.on('open', (({ component, props })) => {
// setModal({
// component
// props,
// close: () => {
// setModal({});
// }
// })
// })
// },[]);
const ModalComponent = (modal as ModalType).component ? (modal as ModalType).component : null;
return (
<ModalContainer>
{
ModalComponent && (
<ModalComponent
{ ...modal.props }
close={ modal.close }
className={ ModalComponent ? 'd-block' : '' }
/>
)
}
</ModalContainer>
);
}
I think there's an error because the modal component can be null.
Is it a problem that the correct object value was not added when setting the model state for the first time?
I thought it’s ambiguous to initialize state to a specific value before receiving props in useEffect.
How should I fix this?
