简体   繁体   English

为什么 React Router 不能使用<Switch> ?

[英]Why wouldn't React Router work with <Switch>?

I'm learning React router, tried to build a pretty basic application like this Home.js :我正在学习 React 路由器,尝试构建一个非常基本的应用程序,例如Home.js

 import React from 'react'; import About from '../pages/About'; import { HashRouter as Router, Switch, Route, Link } from "react-router-dom"; const Navbar = () => { return ( <Router> <div> <nav> <ul> <li> <Link to="/">HOME</Link> </li> <li> <Link to="/about-us">ABOUT US</Link> </li> </ul> </nav> <Switch> <Route path="/about-us"> <About /> </Route> <Route path="/"> <Home /> </Route> </Switch> </div> </Router> ) } const Home = () => { return ( <> <Navbar /> </> ) } export default Home;

Then in index.js render Home page:然后在index.js渲染Home

 import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import Home from './pages/Home'; import * as serviceWorker from './serviceWorker'; ReactDOM.render( <React.StrictMode> <Home /> </React.StrictMode>, document.getElementById('root') ); serviceWorker.unregister();

Problem is, Home wouldn't render in localhost:3000, terminal showed Compiled successfully!问题是, Home不会在 localhost:3000 中呈现,终端显示Compiled successfully! but when loading localhost:3000, it just spins forever with nothing rendered.但是当加载 localhost:3000 时,它只会永远旋转而没有任何渲染。 However, if I remove the element it rendered.但是,如果我删除它呈现的元素。 Folder structure set like this:文件夹结构设置如下:

src
--pages
----About.js
----Home.js
index.js

Can anyone help?任何人都可以帮忙吗?

You've created yourself an infinite loop from what it looks like.你已经为自己创造了一个无限循环。 Your index.js returns your Home component, which then returns the Navbar, which has a router, which then returns the Home component, which then repeats that cycle over and over again.你的 index.js 返回你的 Home 组件,然后返回 Navbar,它有一个路由器,然后返回 Home 组件,然后一遍又一遍地重复这个循环。

This is the approach I would take, which will show the Navbar on both the about and home page, and will only show one of the pages at a time based on the current route.这是我将采用的方法,它将在关于页面和主页上显示导航栏,并且将根据当前路线一次只显示其中一个页面。

// Navbar.js

const Navbar = () => {
    return (
        <nav>
            <ul>
                <li>
                    <Link to="/">HOME</Link>
                </li>
                <li>
                    <Link to="/about-us">ABOUT US</Link>
                </li>
            </ul>
        </nav>
    )
}

export default Navbar;
// Layout.js

import React from 'react';
import Navbar from './Navbar';
import About from '../pages/About';
import Home from '../pages/Home';
import { HashRouter as Router, Switch, Route, Link } from "react-router-dom";

const Layout = () => {
    return (
        <Router>
            <Navbar />

            <Switch>
                <Route path="/about-us">
                    <About />
                </Route>
                <Route path="/">
                    <Home />
                </Route>
            </Switch>
        </Router>
    )
}

export default Layout;
// index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import Layout from './components/Layout';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(
  <React.StrictMode>
    <Layout />
  </React.StrictMode>,
  document.getElementById('root')
);

serviceWorker.unregister();
src
--components
----Navbar.js
----Layout.js
--pages
----About.js
----Home.js
index.js

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

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