1

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

2 Answers 2

4

Try setting the 'component' attribute with lowercase c

Sign up to request clarification or add additional context in comments.

1 Comment

Also in case you run into this problem later, you can also send in props to the component by making it a function like this: component = {() => <Index user = {this.state.loggedIn}/>}
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.