Skip to content

Commit

Permalink
fix: 해시라우팅 시 nav 변하지 않는 오류 수정, store subscriber 중복 문제 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
dosilv committed Dec 19, 2024
1 parent dcb24d3 commit c1ad935
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/app/router.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import Routes from "./routes";
import userService from "../features/UserService";
import { Store } from "../features";

const store = Store.getInstance();

export const historyRouter = (path) => {
store.clearListeners();

const pathToGo = interceptor(path);

history.pushState({}, "", pathToGo);
Expand All @@ -14,6 +19,8 @@ export const historyRouter = (path) => {
};

export const hashRouter = (hash) => {
store.clearListeners();

const path = hash.replace("#", "");
const pathToGo = interceptor(path);

Expand Down
4 changes: 4 additions & 0 deletions src/features/Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const Store = (function () {
listeners.add(listener);
return () => listeners.delete(listener);
},

clearListeners: () => {
listeners.clear();
},
};
};

Expand Down
4 changes: 3 additions & 1 deletion src/widgets/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const Header = () => {
: { href: "/login", title: "로그인" },
];

const currentPath = window.location.pathname;
const currentPath =
window.location.hash?.replace("#", "") || window.location.pathname;

const isCurrNav = (path) => path === currentPath;
const navTextColor = (path) => (isCurrNav(path) ? "blue-600" : "gray-600");
const navFontWeight = (path) => (isCurrNav(path) ? "bold" : "");
Expand Down

0 comments on commit c1ad935

Please sign in to comment.