FrontEnd
[React] 뒤로가기, 홈으로 가기 History 이용
mingg123
2020. 12. 7. 19:57
뒤로 갈때
this.props.history.goBack();
홈으로 갈때
this.props.history.push("/"); 를 사용한다.
간단한 예제로는
import { Component } from 'react';
class HistorySample extends Component {
handleGoBack = () => {
this.props.history.goBack();
};
handleGoHome = () => {
this.props.history.push('/');
};
componentDivMount() {
this.unblock = this.props.history.block('정말 떠나실 건가요?');
}
ccomponentWillUnmount() {
if (this.unblock) {
this.unblock();
}
}
render() {
return (
<div>
<button onClick={this.handleGoBack}>뒤로</button>
<button onClick={this.handleGoHome}>홈으로</button>
</div>
);
}
}
export default HistorySample;
출처 : github.com/velopert/learning-react
velopert/learning-react
[길벗] 리액트를 다루는 기술 서적에서 사용되는 코드. Contribute to velopert/learning-react development by creating an account on GitHub.
github.com