[英]How to prevent react-route from re-render the whole app when changing url
I'm working on an app use react-history. 我正在研究一个应用程序的使用反应历史。 The whole app get re-rendered every time i change the URL by using:
每当我使用以下方法更改URL时,整个应用程序都会重新呈现:
props.history.location.replace(newPath);
That behavior make a great impact to performance. 这种行为会对性能产生重大影响。
What i want is just change the URL which update some query params so user can share the view with another one just by the url. 我想要的只是更改更新一些查询参数的URL,以便用户可以仅通过url与另一视图共享视图。
I wonder if there are a way to work around this problem. 我想知道是否有解决此问题的方法。
Thanks for advice. 谢谢你的建议。
You need to wrap your component inside a common component where you had specified your layout. 您需要将组件包装在指定布局的公共组件内。 Only the body of the layout needs to be re-rendered.
仅布局主体需要重新渲染。
//App.js
import Layout from "../LayoutContainer/Layout"; // where i designed the application's layout
class App extends Component {
render() {
return (
<Layout>
<Router >
<div>
<Route path="/home" exact component={Dashboard} />
<Route path="/product" exact component={Products} />
</div>
</Router>
</Layout>
);
}
}
By using this way, only the content inside the layout would be re-rendered unless the whole application. 通过这种方式,除非整个应用程序,否则仅重新布局中的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.