简体   繁体   English

反应路由器组件没有被渲染

[英]React Router Components not being rendered

I know this question has been asked tons of times but I can't seem to find a working solution for a very simple problem.我知道这个问题已经被问过很多次了,但我似乎无法为一个非常简单的问题找到一个可行的解决方案。

I am setting up a simple Navigation Bar using React Bootstrap and React Router我正在使用 React Bootstrap 和 React Router 设置一个简单的导航栏

The page loads on the Home page but when a link is clicked the URL changes but the component does not render.该页面在主页上加载,但是当单击链接时,URL 会发生变化,但组件不会呈现。 When I inspect using google Chrome, the heading is unchanged.当我使用谷歌浏览器检查时,标题没有改变。

Any help would be greatly appreciated.任何帮助将不胜感激。 I've tried almost everything I found here, used withRouter, used regular and我已经尝试了我在这里找到的几乎所有东西,与Router一起使用,使用常规和

  • elements instead of the ones from React Bootstrap.元素,而不是来自 React Bootstrap 的元素。

    Here is the code, index.js is the regular create-react-app set up这是代码,index.js 是常规的 create-react-app 设置

    App.js应用程序.js

     import React from 'react'; import { Home } from './components/pages/Home'; import Navibar from './components/Navibar'; import { LostPets } from './components/pages/LostPets'; import { AdoptPets } from './components/pages/AdoptPets'; import { ContactUs } from './components/pages/ContactUs'; import './App.css'; import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; function App() { return ( <div className="App"> {/* Navigation Bar */} <Router> <Navibar /> {/* Routes for Pages */} <Switch> <Route exact path='/LostPets' component={LostPets}/> <Route exact path='/AdoptPets' component={AdoptPets}/> <Route exact path='/ContactUs' component={ContactUs}/> <Route exact path='/' component={Home}/> </Switch> </Router> </div> ); } export default App;

    Navbar.js导航栏.js

     import React from 'react'; import { Navbar, Nav, Container } from 'react-bootstrap'; function Navibar () { return( <Container> <Navbar bg="primary" variant="dark" sticky="top"> <Navbar.Brand href="#home">PETSTT </Navbar.Brand> {/* Navigation Bar items */} <Nav className="mr-auto"> <Nav.Link href="#home">Home </Nav.Link> <Nav.Link href="#lostpets">Lost Pets </Nav.Link> <Nav.Link href="#adoptpets">Adopt Me </Nav.Link> <Nav.Link href="#contactus">Contact Us </Nav.Link> </Nav> </Navbar> </Container> ); }

    All pages are just header text like this.所有页面都只是像这样的 header 文本。

     import React from 'react'; export const Home = () => { return ( <div> <h1> Home </h1> </div> ); }

    如果有帮助,我的文件结构

  • You Link is going to the wrong url.Link到错误的 url。 You are going to a hash url which would suggest a different section on the same page with that ID.您将转到 hash url ,这将在同一页面上建议具有该 ID 的不同部分。

    <Nav.Link href="#lostpets">Lost Pets </Nav.Link>
    

    This doesn't match the routes that you are trying to render.这与您尝试渲染的路线不匹配。 The routes are done correctly.路线正确完成。

    <Route exact path='/LostPets' component={LostPets}/>
    

    You should change the Link to this:您应该将Link更改为:

    <Nav.Link href="/LostPets">Lost Pets </Nav.Link>
    

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

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