简体   繁体   English

反应路由器dom不路由

[英]React Router dom not routing

I am building simple application on Codesandbox.io and I am struggling with doing routes, actually I am following tutorials and it just doesn't work with me. 我在Codesandbox.io上构建了一个简单的应用程序,并且正在为路由而苦苦挣扎,实际上我正在学习教程,但对我来说不起作用。

Here is my code, it contains of four files (index, Start, Home and Nav), These files has nothing except just to show a word of (Home or start here), here is my main file (index.js) 这是我的代码,它包含四个文件(index,Start,Home和Nav),这些文件除了显示一个单词(Home或Start here)外什么都没有,这是我的主文件(index.js)

import React from "react";
import ReactDom from "react-dom";
import { BrowserRouter as Router, Route } from "react-router-dom";

import "./styles.css";
import "./news.css";
import Nav from "./Nav";
import Home from "./Home";
import Start from "./Start";

class Index extends React.Component {
  render() {
    return (
      <div>
        <div className="container"> start here </div>
        <Router>
          <div>
            <Nav />
            <Route exact path="/" Component={Index} />
            <Route path="/Home" Component={Home} />
            <Route path="/Start" Component={Start} />
          </div>
        </Router>
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDom.render(<Index />, rootElement);

This is my Home.js 这是我的Home.js

import React from "react";

class Home extends React.Component {
  render() {
    return <div>home</div>;
  }
}

export default Home;

Nav.js Nav.js

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

const Nav = () => {
  return (
    <div>
      <Link to="/">Home</Link>
      <Link to="/Home"> home 2 class </Link>
      <Link to="/Start"> Start </Link>
    </div>
  );
};

export default Nav;

Start.js Start.js

import React from "react";

class Start extends React.Component {
  render() {
    return <div>start here</div>;
  }
}

export default Start;

You were close, you have Component={Home} capitalized, you should have component lower case, like so. 你接近,你有Component={Home}资本,你应该有component较低的情况下,像这样。

I can't seem to edit your sandbox, as Index is breaking the code for me, however, in general, that seems to be your issue. 我似乎无法编辑您的沙箱,因为Index为我破坏了代码,但是,总的来说,这似乎是您的问题。

Alternatively, you could have tried to use the render method to display the corresponding components - Route Render Methods 另外,您可能尝试使用render方法显示相应的组件-Route Render Methods

Cheers! 干杯! Jimmy 吉米

You missed switch , you have to use switch to route. 您错过了switch ,您必须使用switch进行路由。

import switch like this. 像这样的导入开关。

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

Then update render with switch 然后使用开关更新渲染

  <Router>
    <Switch>
        <div className="container">
            <Nav />
            <Route exact path="/" Component={Index} />
            <Route path="/Home" Component={Home} />
            <Route path="/Start" Component={Start} />
        </div>
    </Switch>
</Router>

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

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