简体   繁体   English

在 react-router 上链接后组件不会重新渲染

[英]The component is not re-rendered after linking on react-router

I have a dropdown menu (maked on select/option) in my header.我的 header 中有一个下拉菜单(在选择/选项上创建)。 As planned, when user choose item in dropdown menu, react-router will change URL and make rerender page.按照计划,当用户在下拉菜单中选择项目时,react-router 将更改 URL 并重新渲染页面。 I have a mistake on last stage.我在最后阶段有一个错误。 URL changes, but page not rerender URL 更改,但页面未重新呈现

Link to SandBox 沙盒链接

All Route and Links Components should by wrapped by BrowserRouter, easiest way to do that is to wrap everything in your App.js return statement by BrowserRouter Component.所有 Route 和 Links 组件都应该由 BrowserRouter 包装,最简单的方法是通过 BrowserRouter 组件将所有内容包装在 App.js 返回语句中。

import React from "react";
import { BrowserRouter as Router, Route } from "react-router-dom";
import Header from "./components/Header";
import Stats from "./pages/Stats/Stats";

export default function App() {
  return (
    <Router>
      <div className="App">
        <Header />

        <Route path="/stats" exact>
          <Stats />
        </Route>
      </div>
    </ Router>
  );
}

You must use a <Swtich> with routes to select where the matched routes will rendered.您必须将<Swtich>与到 select 的路由一起使用,匹配的路由将在其中呈现。 And wrap all of that in the <BrowserRouter> or <HashRouter> .并将所有这些包装在<BrowserRouter><HashRouter>中。

<div className="App">
  <Router>
    <Header />

    <Switch>
      <Route path="/stats" component={Stats}></Route>
    </Switch>
  </Router>
</div>

Link to the sandbox 链接到沙盒

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM