简体   繁体   English

React Router 仅在刷新页面后才起作用

[英]React Router works only after refreshing the page

I am a bit new to react and I am facing a problem with the Router.我对反应有点陌生,我遇到了路由器的问题。 The routing works when I enter the URL directly on the browser, however when I click on the link, the URL changes on the browser (eg http://localhost:8080/contactus), but the content don't get updated (But if I do a refresh, it gets updated).当我直接在浏览器上输入 URL 时,路由有效,但是当我单击链接时,浏览器上的 URL 会发生变化(例如 Z80791B3AE7002CB88C246876D9FAAcontactus8F8Z:如果我刷新,它会更新)。

Here is my github repo: https://github.com/Arefaat18/Project这是我的 github 回购: https://github.com/Arefaat18/Project

Here is my MainComponent.js这是我的 MainComponent.js

import React, { Component } from 'react';
import {TransitionGroup, CSSTransition} from 'react-transition-group';
import {Switch, Route, Redirect,withRouter,BrowserRouter as Router} from 'react-router-dom';
import Header from './HeaderComponent';
import ContactUs from './ContactUsComponent';
import AboutUs from './AboutUsComponent';
import Home from './HomeComponent.js';
import {connect} from 'react-redux';  

class Main extends Component {

  constructor(props) {
    super(props);

  }

  
  
  render() {

    return (
      <div>
        <Router>
        <Header />
        <TransitionGroup>
          <CSSTransition classNames="page" timeout={300}>
            <Switch>
              <Route path="/home" component={Home} />
              <Route exact path = "/contactus" component ={ContactUs} />
              <Route exact path = "/aboutus" component ={AboutUs} />
              <Redirect to="/home" />
            </Switch>
          </CSSTransition>
        </TransitionGroup>
        </Router>
        </div>
    );
  }
}

export default withRouter(Main);

And here is my App.js file这是我的 App.js 文件

import React, { Component } from 'react';
import Main from './components/MainComponent';
import './App.css';
import {BrowserRouter} from 'react-router-dom';
import {Provider} from 'react-redux';
import {ConfigureStore} from './components/configureStore';

const store = ConfigureStore();

class App extends Component {


  render() {
    return (
      <Provider store={store}>
        <BrowserRouter>
          <div className="App">
            <Main />
          </div>
        </BrowserRouter>
      </Provider>
    );
  }
}

export default App;

and here is the relevant dependancies这是相关的依赖项

"react": "^17.0.1",
    "react-bootstrap": "^1.4.0",
    "react-dom": "^17.0.1",
    "react-redux": "^7.2.1",
    "react-redux-form": "^1.16.14",
    "react-router-dom": "^5.2.0",
    "react-scripts": "3.4.4",

And here is my ContactUsComponent, the other components are just the same with different text这是我的 ContactUsComponent,其他的组件都是一样的,只是文字不同

import React, { Component } from 'react';

class ContactUs extends Component {

    render(){
        console.log("RENDER");
        return (
            <div>
                <h1> Contact Us</h1>
            </div>
        );
    }
}

export default ContactUs;

Thanks in advance.提前致谢。

Right, you've extraneous Router s declared, your header component has one.是的,您已经声明了无关的Router ,您的标头组件有一个。 Remove this Router .删除此Router

It is because each Router provides a routing context, and each component subscribing to a routing context gets the context from the closest router.这是因为每个Router提供一个路由上下文,而每个组件订阅路由上下文会从最近的路由器的上下文。 The router providing context to the header wasn't rendering any routes so that is why the URL would change but the router actually rendering the Route s wasn't being notified that it should render a different path.向标头提供上下文的路由器没有呈现任何路由,因此这就是 URL 会更改的原因,但实际上呈现Route的路由器并未被通知它应该呈现不同的路径。

class Header extends Component {
  constructor(props) {
    ...
  }

  ...

  render() {
    return (
      <React.Fragment>
        {/* <Router> */} // <-- Remove this Router Component
        <Navbar dark expand="md">
          ...
        </Navbar>
        <Modal isOpen={this.state.isModalOpen} toggle={this.toggleModal}>
          ...
        </Modal>
        {/* </Router> */}
      </React.Fragment>
    );
  }
}

编辑 react-router-works-only-after-refreshing-the-page

I ran into a similar problem.我遇到了类似的问题。 I used Link in the components, but I imported it from @reach/router .我在组件中使用了Link ,但我从@reach/router导入了它。 As a result, I replaced结果我换了

import { Link } from "@reach/router"

with

import { Link } from "react-router-dom"

in each component;在每个组件中; and everything worked as it should一切正常

I just finish the course Front-End Web Development with React.我刚刚完成了使用 React 进行前端 Web 开发课程。 I think you are learning this course too so I see nothing wrong with your code, possibly the problem is inside your ContactUs component.我认为您也在学习这门课程,所以我认为您的代码没有任何问题,可能问题出在您的 ContactUs 组件中。 If you can provide your ContactUs code then I can debug it如果您可以提供您的 ContactUs 代码,那么我可以调试它

I had the same issue as well but I had to wrap my App.js with BrowseRouter in the index.js file.我也遇到了同样的问题,但我必须在index.js文件中用BrowseRouter包装我的App.js

import {BrowserRouter as Router} from 'react-router-dom'
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <Router>
    <App />
  </Router>
);

Well I also stuck with the same Issue but in react 18 our component in index.js file is by default wrapped in react strict mode好吧,我也遇到了同样的问题,但在 react 18 中,我们在 index.js 文件中的组件默认包装在 react 严格模式下

like this:像这样:

<React.StrictMode>
<App/>
</React.StrictMode>

I removed React strict Mode tags then it worked perfectly fine for me我删除了 React 严格模式标签,然后它对我来说非常好用

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

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