less than 1 minute read

오늘 한 작업

우리 웹페이지의 “상세보기” 라는 페이지에서

Nav bar의 로그아웃을 클릭했을 때 해당 페이지를 유지하도록 만들고 싶었다.

기존에는 로그아웃 태그에

link를 root path인 “/” 으로 연결해 홈으로 이동하도록 작성했는데,

react-router-dom 에서 useLocation 을 import 한 뒤,

const Nav = () => {
  const location = useLocation();
  const currentUrl = location.pathname;
...
  <Link
    to={currentUrl}
    onClick={() => {
      logoutHandler();
      window.location.href(currentUrll);
    }}
  >
    로그아웃
  </Link>

위와 같이 location.pathname 을 통해 현재 path name을 가져와

link로 걸어주었더니 현재 페이지는 유지 하면서 로그아웃이 되게 구현할 수 있었다.

Updated: