简体   繁体   English

react-router-dom 不在路由更改时渲染组件

[英]react-router-dom not rendering components on route change

If I click on a navigation link, I can see that the slug has changed but the component is not rendering.如果我单击导航链接,我可以看到 slug 已更改,但组件未呈现。 I have to manually refresh the page in order to see the component, while the page should re-render by itself on route/slug change.我必须手动刷新页面才能看到组件,而页面应该在路由/slug 更改时自行重新渲染。

App.js应用程序.js

import React, { Component } from 'react';
import { BrowserRouter, Switch, Route } from 'react-router-dom';
import Me from './components/me';
import Contrib from './components/contrib';
import Projects from './components/projects';
import Contact from './components/contact';

class App extends Component {
  render() {
    return (
      <div className="container">
        <BrowserRouter>
          <Switch>
            <Route exact path="/" component={Me} />
            <Route path="/contrib" component={Contrib} />
            <Route path="/projects" component={Projects} />
            <Route path="/contact" component={Contact} />
          </Switch>
        </BrowserRouter>
      </div>
    );
  }
}

header.jsx header.jsx

import { BrowserRouter, NavLink} from 'react-router-dom';

const Header = () => {
    return (
        <div className="container">
            <div id="logo">
                <img src={Earth} alt="placeholdit" className="rounded" />
            </div>
            <nav>
                <BrowserRouter>
                    <ul>
                        <li><NavLink to="/">Home</NavLink></li>
                        <li><NavLink to="/contrib">Contributions</NavLink></li>
                        <li><NavLink to="/projects">Projects</NavLink></li>
                        <li><NavLink to="/contact">Contact</NavLink></li>
                        <li></li>
                    </ul>
                </BrowserRouter>
            </nav>
        </div>
    )
}

If I remove the <BrowserRouter> from header.jsx I get an error telling me that: "You should not use <Route> or withRouter() outside a <Router> " .如果我从header.jsx中删除<BrowserRouter> ,我会收到一条错误消息,告诉我: “您不应该在<Router>之外使用<Route>withRouter()

If add the <BrowserRouter> inside of header.jsx the error is gone, but the components are not rendering on route/slug change.如果在 header.jsx 中添加<BrowserRouter> header.jsx则错误消失,但组件不会在路由/slug 更改时呈现。

You need to wrap around the highest order component for it to work as expected.您需要环绕最高阶组件以使其按预期工作。 If your app is simple, wrap it within ReactDOM.render method.如果您的应用程序很简单,请将其包装在 ReactDOM.render 方法中。 Like so像这样

ReactDOM.render((
<BrowserRouter>
  <App/>
</BrowserRouter>),
document.getElementById('root'))

I had the same problem.我有同样的问题。 Delete the from header.jsx and put it in your index.js like this:从 header.jsx 中删除 并将其放入您的 index.js 中,如下所示:

ReactDOM.render(
    <BrowserRouter>
        <App />
    </BrowserRouter>
, document.getElementById('root'));

Be careful with the order of Route , Switch and Router .注意RouteSwitchRouter的顺序 I follow this order and it works for me.我遵循这个顺序,它对我有用。 Note: Try to use the Arrow function注意:尝试使用箭头函数

import React from "react";
import { Switch, Route, Router } from "react-router-dom";
// Containers
import { Dashboard, CardPayment } from "../Containers";

const history = createBrowserHistory();

const ReactRouter = () => {
  return (
    <Router history={history}>
      <Switch>
        <Route path="/dashboard" component={Dashboard} exact={true} />
        <Route path="/card-payment" component={CardPayment} exact={true} />
        <Route path="/" component={Dashboard} exact={true} />
      </Switch>
    </Router>
  );
};

export default ReactRouter;

In my case, a Router was used, and downgrading the "history" library fixed the problem.在我的例子中,使用了路由器,降级“历史”库解决了这个问题。

app.tsx应用程序.tsx

<Router history={history}>
  <Switch>
    <Route exact path={`${AppRoute.ALBUMS}/:author/:album`} component={PhotosPage}/>
    <Route exact path={`${AppRoute.ALBUMS}/:id`} component={AlbumsPage}/>
    <Route path={AppRoute.ERROR} component={ErrorPage}/>
    <Route path={AppRoute.ROOT} component={AuthorsPage}/>
  </Switch>
</Router>

Versions in my project:我的项目中的版本:

package.json:包.json:

...
"history": "4.10.1",
"react-router-dom": "5.2.0",
...

The "history" library was originally version "5.0.0" “历史”库最初是“5.0.0”版本

For those who couldn't fix this error对于那些无法修复此错误的人

Step 1: Make sure the higher order component wrapped with Router.步骤1:确保高阶组件用Router包裹。 In the above question it's already done.在上面的问题中它已经完成了。

class App extends Component {
  render() {
    return (
      <div className="container">
        <BrowserRouter>
          <Switch>
            <Route exact path="/" component={Me} />
            <Route path="/contrib" component={Contrib} />
            <Route path="/projects" component={Projects} />
            <Route path="/contact" component={Contact} />
          </Switch>
        </BrowserRouter>
      </div>
    );
  }
}

Let's consider <Header/> sits inside the any of the components in the <Route/>让我们考虑<Header/>位于<Route/>的任何组件内

Step 2: If higher order component already wrapped with Router , don't wrap the <Header/> also, if you don't want separate routing inside the <Header/> .步骤2:如果高阶分量已经与包裹Router ,不裹<Header/>另外,如果你不希望内部的单独的路由<Header/>

import { NavLink} from 'react-router-dom';

const Header = () => {
    return (
        <div className="container">
            <div id="logo">
                <img src={Earth} alt="placeholdit" className="rounded" />
            </div>
            <nav>
                <ul>
                        <li><NavLink to="/">Home</NavLink></li>
                        <li><NavLink to="/contrib">Contributions</NavLink></li>
                        <li><NavLink to="/projects">Projects</NavLink></li>
                        <li><NavLink to="/contact">Contact</NavLink></li>
                        <li></li>
                    </ul>
            </nav>
        </div>
    )
}

Check the Index.js, Remove the Strict Mode and the render.检查 Index.js,删除严格模式和渲染。

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
//Remove these 
  // <React.StrictMode>
  //   <App />
  // </React.StrictMode>
// Add these
  <BrowserRouter>
    <App/>
  </BrowserRouter>
);
<Link to ={"/home"}>Home</Link>

在我的情况下,我忘记在“to”参数中添加括号,然后它正在渲染组件

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

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