react-router-dom v6 with TS

yezee·2023년 4월 21일
0

TS

목록 보기
4/5
post-thumbnail

v6에서는 제네릭 지원을 하지 않는다 🍄

v5

이전 react-router-dom v5에서는 제네릭을 지원하였기 때문에
interface를 만들어서 제네릭으로 명시해주면 ts가 동작

interface RouterState {
    name: string;
}

function Coin() {
  const location = useLocation<RouterState>() 

v6

interface RouterState {
    name: string;
}

function Coin() {
  const location = useLocation() 
  const name=location.state as RouterState

또는

interface RouterState {
  state: {
    name: string;
  };
}

function Coin() {
  const { state } = useLocation() as RouterState;
profile
아 그거 뭐였지?

0개의 댓글