리액트

react-router 5버전에서 뒤로가기 버튼 만들기(useHistory)

5_hyun 2022. 9. 11. 20:35
반응형

useHistory

react-router 6버전부터는 useNavigate로 구현할 수 있다. 하지만 5버전에서는 useHistory로 구현해야 한다.

 

구현 방법

-import

import { useHistory } from "react-router";

 

-코드

  const history = useHistory();

  const handleHistory = () => {
    history.goBack();
  };

이렇게 goBack() 메서드를 사용하면 된다.

적용하기

          <FontAwesomeIcon icon={faArrowLeft} onClick={handleHistory} />

onClick에 handleHistory 메서드를 주면 된다.

반응형