3
我想我的反應路由器有一些問題。我怎樣才能防止反應路由器重新渲染先前呈現的頁面?如何防止反應路由器重新呈現以前呈現的頁面?
我有路由器這樣的代碼:
class App extends React.Component {
render() {
return (
<div>
<NavBar/>
{this.props.children}
</div>
);
}
}
ReactDOM.render(
(
<Router history={hashHistory}>
<Route path="/" component={App}>
<IndexRoute component={Gateway} />
<Route path="home" component={Gateway} />
<Route path="categories" component={Categories} />
</Route>
</Router>
), document.getElementById('my-app')
);
當我第一次訪問該頁面,它擊中指數,Gateway組件得到了呈現。然後我點擊「類別」鏈接,類別組件得到渲染。然後,當我再次點擊「home」鏈接時,組件網關被重新渲染。它的狀態得到了RESET。這真是令人沮喪,因爲我無法弄清楚爲什麼它的狀態得到重置。
有沒有解決方案?
如果您沒有使用Redux等任何狀態管理,那麼您必須查找正確的方式來使用React共享路由之間的狀態。看看這個帖子中的答案:http://stackoverflow.com/questions/41108701/react-router-share-state-between-routes-without-redux或谷歌的東西,如「反應路由器之間的路由器共享狀態」 – Jayce444