简体   繁体   English

更改网址时如何防止React-Route重新呈现整个应用程序

[英]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.

相关问题 React - 删除元素时防止重新呈现整个列表 - React - Prevent re-render whole list when delete element 在 React 上使用 sockets 时如何防止重新渲染? - How to prevent re-render when using sockets on React? 如何防止反应子组件重新渲染? - How to prevent react child components from re-render? 如何防止反应在重新渲染时自动滚动页面? - How to prevent react from auto scrolling the page on re-render? 重新渲染时,React应用程序崩溃 - React app crashes when re-render React:如何重构它以防止整个重新渲染并将其封装到重要的组件中? - React: How to refactor this to prevent a whole re-render and encapsulate it towards the component that matters? 如何保留 state 并防止更改层次结构后重新渲染反应组件? - How to keep state and prevent re-render of react component after changing hierarchy? 如何防止使用 React-Router 和 Framer Motion 重新渲染父路由? - How to prevent re-render of parent route using React-Router and Framer Motion? 使用 i18n 更改应用程序语言时如何强制整个应用程序重新呈现? - How to force the whole app re-render when change app language with i18n? 如何使用React Router路由到另一个URL并使用NEW组件重新呈现页面? - How to use React Router to route to another url AND re-render page with NEW component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM