1-1. 잘못된 주소 처리하기
import React from "react";
const NotFound = (props) => {
return <h1>주소가 올바르지 않아요!</h1>;
};
export default NotFound;
import NotFound from "./NotFound";
...
import { Route, Switch } from "react-router-dom";
...
class App extends React.Component {
...
render() {
return (
<div className="App">
...
<Switch>
<Route
path="/"
exact
render={(props) => (
<BucketList
list={this.state.list}
history={this.props.history}
/>
)}
/>
<Route path="/detail" component={Detail} />
</Switch>
...
</div>
);
}
...
<Switch>
<Route
path="/"
exact
render={(props) => (
<BucketList
list={this.state.list}
history={this.props.history}
/>
)}
/>
<Route path="/detail" component={Detail} />
<Route component={NotFound} />
</Switch>
...
1-2. NotFound에 뒤로가기 버튼을 달아보자.
<Route render={(props) => (
<NotFound
history={this.props.history}
/>
)}/>
import React from "react";
const NotFound = (props) => {
return (
<div>
<h1>주소가 올바르지 않아요!</h1>
<button>뒤로가기</button>
</div>
);
};
export default NotFound;
const NotFound = (props) => {
console.log(props);
...
<button onClick={() => {props.history.goBack();}}>뒤로가기</button>