Skip to content

Commit

Permalink
refactor: initialize modal root in modal portal
Browse files Browse the repository at this point in the history
- create modal root at initial rendering
- set ref initial value as null

Refs: #649 (comment) #649 (comment)
  • Loading branch information
greenblues1190 committed Oct 10, 2022
1 parent 9fd2363 commit e53b044
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 52 deletions.
97 changes: 47 additions & 50 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,57 +26,54 @@ import ModalProvider from "./provider/ModalProvider";

const App = () => {
return (
<>
<TokenProvider>
<RecruitmentProvider>
<ModalProvider>
<BrowserRouter>
<Header />
<main className="main">
<ScrollToTop>
<Routes>
<Route path={PATH.HOME} element={<Recruits />} />
<Route path={PATH.RECRUITS} element={<Recruits />} />
<Route path={PATH.SIGN_UP} element={<SignUp />} />
<Route path={PATH.LOGIN} element={<Login />} />
<Route path={PATH.FIND_PASSWORD} element={<PasswordFind />} />
<Route path={PATH.FIND_PASSWORD_RESULT} element={<PasswordFindResult />} />
<Route element={<PrivateRoute />}>
<Route path={PATH.APPLICATION_FORM} element={<ApplicationRegister />} />
<Route path={PATH.EDIT_PASSWORD} element={<PasswordEdit />} />
<Route path={PATH.MY_APPLICATION} element={<MyApplication />} />
<Route path={PATH.ASSIGNMENT} element={<AssignmentSubmit />} />
</Route>
<TokenProvider>
<RecruitmentProvider>
<ModalProvider>
<BrowserRouter>
<Header />
<main className="main">
<ScrollToTop>
<Routes>
<Route path={PATH.HOME} element={<Recruits />} />
<Route path={PATH.RECRUITS} element={<Recruits />} />
<Route path={PATH.SIGN_UP} element={<SignUp />} />
<Route path={PATH.LOGIN} element={<Login />} />
<Route path={PATH.FIND_PASSWORD} element={<PasswordFind />} />
<Route path={PATH.FIND_PASSWORD_RESULT} element={<PasswordFindResult />} />
<Route element={<PrivateRoute />}>
<Route path={PATH.APPLICATION_FORM} element={<ApplicationRegister />} />
<Route path={PATH.EDIT_PASSWORD} element={<PasswordEdit />} />
<Route path={PATH.MY_APPLICATION} element={<MyApplication />} />
<Route path={PATH.ASSIGNMENT} element={<AssignmentSubmit />} />
</Route>

<Route element={<PrivateRoute />}>
<Route
path={PATH.MY_PAGE}
element={
<UserInfoProvider>
<MyPage />
</UserInfoProvider>
}
/>
<Route
path={PATH.EDIT_MY_PAGE}
element={
<UserInfoProvider>
<MyPageEdit />
</UserInfoProvider>
}
/>
</Route>
</Routes>
</ScrollToTop>
</main>
<Footer />
<InquiryFloatingButton />
</BrowserRouter>
</ModalProvider>
</RecruitmentProvider>
</TokenProvider>
<div id="modal-root" />
</>
<Route element={<PrivateRoute />}>
<Route
path={PATH.MY_PAGE}
element={
<UserInfoProvider>
<MyPage />
</UserInfoProvider>
}
/>
<Route
path={PATH.EDIT_MY_PAGE}
element={
<UserInfoProvider>
<MyPageEdit />
</UserInfoProvider>
}
/>
</Route>
</Routes>
</ScrollToTop>
</main>
<Footer />
<InquiryFloatingButton />
</BrowserRouter>
</ModalProvider>
</RecruitmentProvider>
</TokenProvider>
);
};

Expand Down
11 changes: 9 additions & 2 deletions frontend/src/components/Modal/ModalPortal/ModalPortal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ type ModalPortalProps = React.PropsWithChildren<{
}>;

const ModalPortal = ({ children, closeModal }: ModalPortalProps) => {
const ref = useRef<Element | null>();
const ref = useRef<Element | null>(null);
const [mounted, setMounted] = useState(false);

useEffect(() => {
setMounted(true);

const modalRoot = document.getElementById("modal-root");
let modalRoot = document.getElementById("modal-root");

if (!modalRoot) {
modalRoot = document.createElement("div");
modalRoot.id = "modal-root";
document.body.appendChild(modalRoot);
}

ref.current = modalRoot;
}, []);

Expand Down

0 comments on commit e53b044

Please sign in to comment.