mingg IT

[React] 뒤로가기, 홈으로 가기 History 이용 본문

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

 

'FrontEnd' 카테고리의 다른 글

[React] 웹팩(webpack) 과 create-react-app  (0) 2020.12.14
[React] Context API  (0) 2020.12.08
[React, JavaScript] 비동기 처리  (0) 2020.12.08
[React] NavLink  (0) 2020.12.08
[React] Error: node sass version 5.0.0 is incompatible with ^4.0.0.  (0) 2020.12.05
Comments