[React] useHistory

2021. 7. 1. 12:18React

react-router-dom의 hooks를 사용하여 경로를 설정해보자

사용할 hooks는 useHistory이다.

예제에서 링크를 버튼으로 만든 뒤, onClick 이벤트를 걸어서 경로를 변경시킬 것이다.

import { useHistory } from "react-router-dom";

useHistory를 import 해준다.

const history = useHistory();
<button onClick={() => history.push('/List')}>List 이동</button>

버튼을 클릭 시 해당하는 컴포넌트로 이동하게 된다.

'React' 카테고리의 다른 글

[React] 이미지 파일 업로드 express 서버 송신  (0) 2021.08.02
[React] 소셜 로그인 API  (0) 2021.07.06
[React] React Query 사용하기  (0) 2021.07.01
react 성능 최적화  (0) 2021.06.25
<Switch>는 언제 써야 할까?  (0) 2021.06.23