코딩쌀롱

2021.5.30(sat)_window.location.search 본문

회고/하루 기록

2021.5.30(sat)_window.location.search

이브✱ 2021. 5. 30. 03:50

console.dir(location)해봤을 때.

- location.pathname: 요청한 url의 path값

- match.path: 정의한 라우트의 path값

- match.isExact: 요청한 path와 정의한 라우트의 path가 같은지

- location.search: 쿼리 스트링

 

window.location.search를 queryString.parse에 전달해주면 객체로 만들어준다.

location.search하니까 리액트+타입스크립트에서 에러가 나서 window.을 붙여줬더니 해결됐다.

 

import queryString from "query-string";

const MyPageModal = ({ onClick }: Props) => {
  const query = queryString.parse(window.location.search);
  console.log(query.code)
  return(
    <div>
      {query.code}
    </div>
  );
};

 

 


참고📚

블로그 - 리액트 라우터로 path와 querystring 받아오기

 

 

 

 

Comments