简体   繁体   English

除非重新加载页面,否则 React 应用程序路由不会加载内容

[英]React app routing doesn't load content unless page reloaded

I've been working on a react single page app and have been trying to get the routing to work.我一直在研究 React 单页应用程序,并一直试图让路由工作。

I believe the routing itself actually works however the page does not load the correct content unless the page is manually reloaded.我相信路由本身确实有效,但是除非手动重新加载页面,否则页面不会加载正确的内容。 Back and forward browser arrows also work.后退和前进浏览器箭头也有效。

For example I can navigate to localhost:3000 fine and the content is loaded correctly but when I press a button to navigate to localhost:3000/contacts nothing is displayed unless I refresh the page.例如,我可以很好地导航到localhost:3000并且内容已正确加载,但是当我按下按钮导航到localhost:3000/contacts除非刷新页面,否则不会显示任何内容。 Once manually refreshed the contacts page shows up.手动刷新后,将显示联系人页面。 What gives?是什么赋予了?

index.tsx索引.tsx

import React from 'react'
import ReactDOM from 'react-dom'
import { BrowserRouter as Router } from 'react-router-dom';

// Import App component
import App from './App'

// Import service workers
import * as serviceWorker from './serviceWorker'

// Render App component in the DOM
ReactDOM.render(
    <Router>
        <App />
    </Router>
  , document.getElementById('root')
)
serviceWorker.unregister()

App.tsx应用程序.tsx

// Import necessary dependencies
import React from 'react'
import Routes from './Routes'

// Create App component
function App() {
    return (
        <div className="App">
            <Routes />
        </div>
    )
}
export default App

history.tsx历史.tsx

import { createBrowserHistory as history} from 'history';

export default history();

Home/Home.tsx主页/Home.tsx

import React, { Component } from "react";
import history from './../history';
import "./Home.css";

export default class Home extends Component {
    render() {
        return (
            <div className="Home">
                hello home
                <button onClick={() => history.push('/Contact')} value='click here'>Get Started</button>
            </div>
        );
    }
}

Contact/Contact.tsx联系方式/Contact.tsx

import React, { Component } from 'react';

class Contact extends Component {
    render() {
        return (
            <div>
              hello world
            </div>
        );
    }
}

export default Contact;

Routes.tsx路由.tsx

import React, { Component } from "react";
import {BrowserRouter, Router, Switch, Route } from "react-router-dom";

import Contact from "./Contact/Contact";
import Home from "./Home/Home"
import history from './history'

export default class Routes extends Component {
    render() {
        return (
            <div>
                <Router history={history}>
                    <Switch>
                        <Route path="/" exact component={Home} />
                        <Route path="/Contact" component={Contact} />
                    </Switch>
                </Router>
            </div>
        )
    }
}

Any help greatly appreciated非常感谢任何帮助

I think there's some extra code that might be causing conflict.我认为有一些额外的代码可能会导致冲突。 You're defining the Router from react-router-dom twice:您正在从react-router-dom定义路由器两次:

Once here, in index.tsx在这里,在index.tsx

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

and then again in Routes.tsx然后再次在Routes.tsx

<Router history={history}> // here
  <Switch>
    <Route path="/" exact component={Home} />
    <Route path="/Contact" component={Contact} />
  </Switch>
</Router>

You have to drop one of them, they're probably conflicting each other你必须放弃其中之一,它们可能相互冲突

Update更新

In addition to that, I think you should not use the history object directly from your export, but access it through the HOC withRouter .除此之外,我认为您不应直接从导出中使用history对象,而应通过 HOC withRouter访问它。 Then, you'd wrap然后,你会包装

So you'd do something like this所以你会做这样的事情

import React, { Component } from "react";
import { withRouter } from 'react-router-dom'
import "./Home.css";

class Home extends Component {
    const { history } = this.props
    render() {
        return (
            <div className="Home">
                hello home
                <button onClick={() => history.push('/Contact')} value='click here'>Get Started</button>
            </div>
        );
    }
}

export default withRouter(Home)

I think the issue here is that you need to wrap your pages with withRouter() like so:我认为这里的问题是你需要像这样用withRouter()包装你的页面:

import React, { Component } from "react";
import history from './../history';
import "./Home.css";
import { withRouter } from 'react-router-dom'; //<---------- add this

class Home extends Component {
    render() {
        return (
            <div className="Home">
                hello home
                <button onClick={() => history.push('/Contact')} value='click here'>Get Started</button>
            </div>
        );
    }
}

default export withRouter(Home); //<-------------- and add this

You will need to do the same on your Contact page as well.您也需要在“ Contact页面上执行相同操作。

Why do you have two routers?为什么有两个路由器?

I guess you simply need to remove the Router either in index.tsx or in Routes.tsx我想你只需要在 index.tsx 或 Routes.tsx 中删除路由器

According to you file naming I would remove the Router in Routes.tsx根据您的文件命名,我将删除 Routes.tsx 中的路由器

It seems like you are using the history package for navigation.您似乎正在使用history包进行导航。 If you are using the react-router v5 , then you may have to downgrade the history package to 4.10.1 until the history package developers issue a new release with the fix.如果您使用的是react-router v5 ,那么您可能必须将history包降级到4.10.1直到history包开发人员发布带有修复程序的新版本。 As of writing this, the latest release version of history package is v5.0.1 , and if you see a new release than this, you can try with that version first to see if it works, before doing the downgrade to 4.10.1在撰写本文时, history包的最新发布版本是v5.0.1 ,如果您看到比这个新版本,您可以先尝试使用该版本看看它是否有效,然后再降级到4.10.1

Issue Link -> https://github.com/remix-run/history/issues/804问题链接 -> https://github.com/remix-run/history/issues/804

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

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