민프
[React] react-router-dom 에서 URL 파라미터를 받아와보자 본문
만약
/spot/BTCUSDT_SPBL?type=spot
이라는 URL 파라미터를 설정해주기 위해서는 아래와 같이 작성해야한다
<Route path="/spot/:id" element={<SpotTradingPage />} />
이렇게 하게 되면 위의 id값은 BTCUSDT_SPBL이 되는 것 이다.
그럼 뒤에 타입은 어떻게 가지고올까?
useLocation hook을 사용하여 쿼리 파라미터를 받아와 사용할 수 있다
예를 들어서
import { useLocation } from "react-router-dom";
function SpotTradingPage() {
const location = useLocation();
const id = useParams<{ id: string }>().id;
const type = new URLSearchParams(location.search).get("type");
// ...
return (
// ...
);
}
'[React]' 카테고리의 다른 글
[React][TypeScript][MUI] Slider 커스텀을 해보자 (0) | 2023.05.04 |
---|---|
[React][TypeScript][MUI] MUI Switch Toogle버튼의 크기를 변경해보자 (width, height, 동그라미 크기, 동그라미 위치) (0) | 2023.04.06 |
[React][TypeScript] useState 리스트 필터링 및 삭제 (0) | 2023.03.31 |
[React][TypeScript] QR코드를 생성해보자 (feat. qrcode.react) (0) | 2023.03.23 |
[React][TypeScript] npm http-server은 무엇일까? (0) | 2023.03.21 |
Comments