简体   繁体   English

Url 更改但未呈现组件 React Router

[英]Url change but not rendering component React Router

Hi i am trying to change the route of my website to go to Contact, when the menu is clicked.嗨,当单击菜单时,我正在尝试更改我网站的路线以转到联系人。 Whenever i press the button for the homepage which is "Forside", the routing works perfectly.每当我按下主页的“Forside”按钮时,路由就完美无缺。

But as soon as i press the button for the contact which is "Kontakt", the url changes and no component renders.但是,只要我按下“Kontakt”联系人的按钮,网址就会发生变化,并且不会呈现任何组件。 But if i refresh the page, it shows up..但是如果我刷新页面,它会显示..

Any ideas what is wrong with my code, or any way to fix it?任何想法我的代码有什么问题,或者有什么方法可以解决它? All help will be appreciated thanks.感谢所有帮助。

My App.js我的 App.js

import React from 'react';
import {BrowserRouter as Router, Switch, Route} from 'react-router-dom';
import Forside from './forside';
import Kontakt from './Kontakt';
import Header from './Header'

const App = () => {
return(
<Router>
<div>
<Header/>
<Switch>
<Route exact path="/" component={Forside}/>
<Route path="/kontakt" component={Kontakt}/>
</Switch>
</div>
</Router>
)
}

export default App

And this is where i link to the different routes.这是我链接到不同路线的地方。

import React, { useState } from 'react';
import './header.css'
import { makeStyles } from '@material-ui/core/styles';
import BottomNavigation from '@material-ui/core/BottomNavigation';
import BottomNavigationAction from '@material-ui/core/BottomNavigationAction';
import ContactMailIcon from '@material-ui/icons/ContactMail';
import LocationOnIcon from '@material-ui/icons/LocationOn';
import {Link} from 'react-router-dom';

const useStyles = makeStyles({
  root: {
    width: 500,
  },
});

const Header = () => {
  const classes = useStyles();
  const [value, setValue] = React.useState(0);

return(
<div className="hed">
<h1 className="logo"><span>IT</span> ARBEJDE.DK</h1>

<BottomNavigation
  value={value}
  className="nav"
  onChange={(event, newValue) => {
    setValue(newValue);
  }}
  showLabels
  className={classes.root}
>
  <Link to="/">
  <BottomNavigationAction label="Søg job" icon={<LocationOnIcon />} />
  </Link>

<Link to="/kontakt">
<BottomNavigationAction label="Kontakt" icon={<ContactMailIcon/>} />
</Link>
  

</BottomNavigation>
</div>     
)
}

export default Header

add exact to it to your route将其添加到您的路线中

 <Route exact path="/kontakt" component={Kontakt}/>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

Since i didn't get a answer, i have figured it out on my own.由于我没有得到答案,所以我自己弄明白了。 So i would just like to share the solution, for other people who maybe are expecting the same bug.所以我只想分享解决方案,供其他可能期待相同错误的人使用。

I fixed the problem, by restructuring my code.我通过重组我的代码解决了这个问题。 I placed my router inside the index.js component like this:我将路由器放在 index.js 组件中,如下所示:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import {BrowserRouter as Router} from 'react-router-dom';

ReactDOM.render(

  <React.StrictMode>
       <Router>
           <App/>
       </Router>
  </React.StrictMode>,

  
  document.getElementById('root')
);

Then placed the Header code inside my App.js, instead of having the Component rendering.然后将 Header 代码放在我的 App.js 中,而不是让组件呈现。

So i refactored the code like this:所以我重构了这样的代码:

import React from 'react';
import {BrowserRouter as Router, Switch, Route} from 'react-router-dom';
import Kontakt from './Kontakt';
import Forside from './forside';
import { makeStyles } from '@material-ui/core/styles';
import BottomNavigation from '@material-ui/core/BottomNavigation';
import BottomNavigationAction from '@material-ui/core/BottomNavigationAction';
import ContactMailIcon from '@material-ui/icons/ContactMail';
import LocationOnIcon from '@material-ui/icons/LocationOn';
import {Link} from 'react-router-dom';
import './header.css'


const useStyles = makeStyles({
    root: {
      width: 500,
    },
  });


const App = () => {
    const classes = useStyles();
    const [value, setValue] = React.useState(0);

return(
<div>
<div className="header-container">
<h1 className="logo"><span>IT</span> ARBEJDE.DK</h1>
<BottomNavigation
  value={value}
  className="nav"
  onChange={(event, newValue) => {
    setValue(newValue);
  }}
  showLabels
  className={classes.root}
>
  <BottomNavigationAction label="Søg job" component={Link} to="/" icon={<LocationOnIcon />} />
  <BottomNavigationAction label="Kontakt" component={Link} to="/kontakt" icon={<ContactMailIcon/>} />
</BottomNavigation>
</div>     

<Switch>
<Route exact path="/" component={Forside}/>
<Route exact path="/kontakt" component={Kontakt}/>
</Switch>
</div>
)
}

export default App

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

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