Skip to content

Commit

Permalink
refactor: memoization and conditional render
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinho1011 committed Dec 8, 2023
1 parent 553681d commit 7a84c5c
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions src/hooks/useModal/useModal.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import { useState } from 'react';
import { useCallback, useState } from 'react';

import { default as DefaultModal, ActionModal } from '@components/commons/Modal/Modal';
import DefaultModal, { ActionModal } from '@components/commons/Modal/Modal';

const useModal = (type: 'default' | 'action' = 'default') => {
const [isOpen, setIsOpen] = useState(false);

const toggleModal = () => {
const toggleModal = useCallback(() => {
setIsOpen((prev) => !prev);
};
}, []);

const Modal = ({ children }: { children: React.ReactNode }) =>
type === 'default' ? (
<DefaultModal isOpen={isOpen} onClose={toggleModal}>
{children}
</DefaultModal>
) : (
<ActionModal isOpen={isOpen} onClose={toggleModal}>
{children}
</ActionModal>
);
const ModalComponent = type === 'default' ? DefaultModal : ActionModal;

const Modal = ({ children }: { children: React.ReactNode }) => (
<ModalComponent isOpen={isOpen} onClose={toggleModal}>
{children}
</ModalComponent>
);

return { Modal: isOpen ? Modal : () => null, toggleModal };
};
Expand Down

0 comments on commit 7a84c5c

Please sign in to comment.