React Router URL Change but Not Component

痞子三分冷 提交于 2021-02-11 13:23:29

问题


React Router change the URL but the component is not rendered I have already looked for answer but none of those example is worked Current React Router & React Router DOM version is 5.0.0 It's still plain create-react-app

I've tried to use Switch tag

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';

const Index = () => {
  return <h2>Home</h2>;
};

const About = () => {
  return <h2>About</h2>;
};

const Users = () => {
  return <h2>Users</h2>;
};

class App extends Component {
  render() {
    return (
      <Router>
        <div className="App">
          <header className="App-header">
            <li>
              <Link to="/">
                <h1>Home</h1>
              </Link>
            </li>
            <li>
              <Link to="/about">
                <h1>About</h1>
              </Link>
            </li>
            <li>
              <Link to="/users">
                <h1>Users</h1>
              </Link>
            </li>
          </header>
          <hr />
          <Route exact path="/" Component={Index} />
          <Route path="/about" Component={About} />
          <Route path="/users" Component={Users} />
        </div>
      </Router>
    );
  }
}

export default App;

It wont render the component


回答1:


Try setting the 'component' attribute with lowercase c




回答2:


I think it's a simple mistake. You capitalized the attribute word component in your Routes.



来源:https://stackoverflow.com/questions/55340595/react-router-url-change-but-not-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!