'Why does my React Router return blank pages?
I am using the latest version of create-react-app. I have searched all tutorials and tried a few websites with different tutorials but I am not getting errors.
I am trying to return the pages within my app. I have done both functional components and class based components but no look. I have tried wrapping in return but it won't render. All the app works but the pages content do not display :(
About.js and Home.js are the same like this:
import React from 'react';
const About = () => (
<div>
<h2>About</h2>
</div>
);
export default About;
Nav.js is simple enough:
import React from 'react';
import { BrowserRouter as Router, Route, NavLink } from "react-router-dom";
import './css/Nav.css';
const Nav = () => (
<ul>
<li>
<NavLink
activeClassName="active"
to="/">Home</NavLink>
</li>
<li>
<NavLink
activeClassName="active"
to="/about">Admin</NavLink>
</li>
</ul>
);
export default Nav;
And finally the index.js
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import {
BrowserRouter as Router,
Route,
Switch
} from "react-router-dom";
import registerServiceWorker from './misc';
// COMPONENTS
import Nav from './nav/Nav';
// PAGE COMPONENTS
import Home from './pages/Home';
import About from './pages/About';
import Page404 from './pages/404';
// CSS
import './css/Index.css';
// CONSTRUCT
class SmartApartmentApp extends Component {
render() {
return (
<Router>
<div className="App">
<Nav />
<Switch>
<Route exact path="/" Component={Home} />
<Route path="/about" Component={About} />
<Route component={Page404} />
</Switch>
<Footer />
</div>
</Router>
);
}
}
// EXECUTE
export default SmartApartmentApp;
ReactDOM.render(<SmartApartmentApp />, document.getElementById('root'));
registerServiceWorker()
;
Solution 1:[1]
Use component property instead of Component.
Solution 2:[2]
This problem is for using wrong prop name Component
!
To resolve this issue in the Route component pass component
prop using lowercase c
instead of Component
using uppercase C
.
Example:
<Route path="/about" component={About} />
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Igor Stetsiura |
Solution 2 |