我試圖使用React-Router V4將路由添加到我的應用,但它根本無法工作。基本上,我試圖用編程方式更改history.push
的路線,該路線更新瀏覽器URL,但不更改實際應用程序內的任何內容。React路由器V4更新URL但不刷新(React,Redux)
注意:我正在使用redux。
對這個問題的唯一回答的問題是:
React history.push() is updating url but not navigating to it in browser
不過,我試圖回答上述問題,它不會爲我工作。
以下是重要的片段:
最頂層文件(index.js)
...
ReactDOM.render(
<BrowserRouter>
<Provider store={store}>
<App/>
</Provider>
</BrowserRouter>
, document.getElementById('root'));
...
元器件路由
...
export default function ContentRouter() {
return <div className="content">
<Route exact path="/dashboard" component={TmpDashboard}/>
<Route exact path="/" component={() => {
return <h1>Home</h1>
}}/>
</div>
}
組件推路線
...
this.handleGroupClick = (group) => {
this.props.history.push(`/groups/${group}`);
this.props.onOpenChange(false);
};
...
export default withRouter(connect(mapStateToProps, mapDispatchToProps(DrawerConnector))
下面是一個:'this.props.history.push' https://stackoverflow.com/questions/31079081/programmatically-navigate-using-react-router – admcfajn
@admcfajn那是我正在嘗試的那個 – AJC
I在ContentRouter中看不到DrawerConnector。它在哪裏被使用?它嵌套在這些組件之一中嗎?我發現在我的頂層組件上使用路由器並且在其中執行是很有用的。對於他們的嵌套組件,我將歷史作爲道具傳遞。希望這是有道理的。 –
Hossein