Front-end/React

[React]라우터가 아닌 컴포넌트에서 location, match, history 사용하기 - withRouter

냥인 2021. 5. 12. 18:57
728x90
반응형

라우터가 아닌 컴포넌트에서 location, match, history를 사용해야할 때에는 withRouter를 사용한다.

 

const callback = withRouter(() => {

});

 

이렇게 withRouter로 감싸도 되고, export default withRouter(callback); 이렇게 감싸면 된다.

 

그러면 props에서 location, match, history를 받아올 수 있다.

 

* 라우터가 아닌 컴포넌트에서 어떤 이벤트가 발생했을 때 URL을 이동할 일이 있을 때 history.push를 이용해서 URL을 이동하고 싶었기 때문에 withRouter를 찾게 되었다.

 

728x90
반응형