[React] Cannot read property 'params' of undefined 에러 해결하기

소영·2023년 4월 6일
0
const Profile = ({ match }) => {
	const { username } = match.params;
    ...
}

오류가 난 코드는 다음과 같다.
옛날 코드를 보면서 작성중이였는데, 알고보니 react-router-dom이 버전6으로 업그레이드 되면서 파라미터를 받기 위해 match 객체를 사용했던 방식을 더이상 사용하지 않는다는 것이다.

이제 아래의 코드처럼 useParams()를 써주면 된다고 한다.

const Profile = () => {
	const { username } = useParams();
    ...
}

0개의 댓글