更改url时如何防止react-route重新渲染整个应用程序

Sỹ *_* Lê 5 performance reactjs react-router

我正在开发一个使用 react-history 的应用程序。每次我使用以下方法更改 URL 时,整个应用程序都会重新呈现:

props.history.location.replace(newPath);
Run Code Online (Sandbox Code Playgroud)

这种行为对性能有很大的影响。
我想要的只是更改更新一些查询参数的 URL,以便用户可以仅通过 url 与另一个共享视图。
我想知道是否有办法解决这个问题。
谢谢你的建议。

小智 0

您需要将组件包装在已指定布局的公共组件内。只有布局的主体需要重新渲染。

//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>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

通过使用这种方式,只有布局内的内容才会被重新渲染,除非整个应用程序。