個人理解:
<Route exact path="/Home" component={Home}/>
1.路由組件:只有包裹在Route組件里的才能使用`this.props.location`, 2.非路由組件:假如有個需求,是面包屑或者導航組件里需要拿到`this.props.location`(導航組件或者面包屑一般不會包裹在`Route`里吧),那么直接這么寫顯然就不行了。 這個時候`withRouter`修飾一下,就可以這么寫了。
useHistory
import { useHistory } from "react-router-dom"; function HomeButton() { let history = useHistory(); function handleClick() { history.push("/home"); } return ( <button type="button" onClick={handleClick}> Go home </button> ); }
文檔在這里
.