navigation is giving me blank page - reactjs

I used ReactJS a year ago only once just for a small project, however, now I have a problem setting the navigation. The navigation on the old project it's working, I found out there is a new version of react-router-dom, I tried to implement the changes into the code but still, it doesn't work. I don't have a clue why :( any hint is appreciated. Thank you!
Console error:
Uncaught Error: A <Route> is only ever to be used as the child of <Routes> element, never rendered directly. Please wrap your <Route> in a <Routes>.
The above error occurred in the <Route> component:
App.js:
import React from 'react';
import { BrowserRouter as Routes, Route } from "react-router-dom";
import {Nav} from './Nav';
import { Posts } from './Posts';
export function App() {
return (
<div className="App">
<Nav/>
<BrowserRouter>
<Routes>
<Route path="/" element={<App/>}/>
<Route path="/posts" element={<Posts/>}/>
</Routes>
</BrowserRouter>
</div>
);
}
Nav:
import React from 'react';
import {Link} from 'react-router-dom'
import Logo from '../images/Logo.png';
export function Nav(){
return(
<div id="nav">
<img src={Logo} class="nav-logo" alt="logo"/>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/Posts">Posts</Link>
</li>
</ul>
</div>
)
}
index:
import React from 'react';
import ReactDOM from 'react-dom';
import {App} from './components/App';
import {BrowserRouter, BrowserRouter as Router, Route} from 'react-router-dom';
ReactDOM.render(
<BrowserRouter>
<Router>
<Route path='/' element={<App/>}/>
</Router>
</BrowserRouter>
, document.getElementById("root"));

React-Router updated quite a lot, I recommended read the documentation and
don't do anything in the index.js file usually to routing in App.js file
import React from 'react';
import ReactDOM from 'react-dom';
import {App} from './components/App';
ReactDOM.render(
<App />
, document.getElementById("root"));
App.js file
import React from 'react';
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import {Nav} from './Nav';
import { Posts } from './Posts';
export function App() {
return (
<div className="App">
<BrowserRouter>
<Nav/>
<Routes>
<Route path="/posts" element={<Posts/>}/>
<Route path="*" element={<Navigate to="/posts" />} />
</Routes>
</BrowserRouter>
</div>
);
}

Related

React using react-router-dom shows nothing on screen

No error message but it just shows a white screen nothing more. These 3 components matter, the other ones are just unedited snippets (ES6 rafce). The only error I got was useHistory imported but not used, but this one mysteriously disappeared upon restarting the app (npm start). I did change the index a bit before restarting however, but that was just changing component in the Route to element as you can see below.
Edit (because of duplicate question): I know that this route in the index to route the root to app is kind of weird but I tried importing routes as well and enclosing it in it, as well as just using the <App /> directly. Both still gave a white a screen. I think it probably has something to do with the navbar because I was working on it when this problem first occured. However I first added the Links in the navbar only after I added the routes to App and the BrowserRouter to index. So there could be something wrong there as well.
Index component
//index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import { BrowserRouter as Router, Route } from 'react-router-dom';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<Router>
<Route path="/" element={<App />} />
</Router>
</React.StrictMode>
);
App component
//App.js
import Navbar from './components/Navbar';
import Footer from './components/Footer';
import Home from './components/Home';
import AboutUs from './components/AboutUs';
import Contact from './components/Contact';
import Missing from './components/Missing';
import { Route, Routes, useHistory } from 'react-router-dom';
function App() {
return (
<div className="App">
<Navbar />
<Routes>
<Route exact path="/">
<Home />
</Route>
<Route path="/contact">
<Contact />
</Route>
<Route path="/about_us" element={<AboutUs />} />
<Route path="*" element={<Missing />} />
</Routes>
<Footer />
</div>
);
}
export default App;
Navbar component
//Navbar.js
import React from 'react';
import logo from "../images/Odanhu1.png";
import { FaInstagram, FaUser } from 'react-icons/fa';
import { Link } from 'react-router-dom';
const Navbar = () => {
return (
<nav>
<div>
<ul>
<li><Link to="/">
<figure>
<img src={logo} alt="logo" />
<figcaption>danhu</figcaption>
</figure>
</Link></li>
<li><FaInstagram /></li>
<li><Link to="/about_us">About us</Link></li>
<li><Link to="/contact">Contact</Link></li>
<li><p>Partners</p></li>
</ul>
<ul>
<li><Link><FaUser /></Link></li>
</ul>
</div>
<div>
<p></p>
</div>
</nav>
)
}
export default Navbar
You don't need to wrap your App component in a <route> when you render it.
Try this:
//index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import { BrowserRouter as Router } from 'react-router-dom';
//you don't need to routes in index and the app file
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<Router>
<App/>
</Router>
</React.StrictMode>
);

react router not working and no erros in console or compilation error is shown [ react router dom v5]

Until now the router was working perfectly fine but suddenly it isn't working. I'm totally confused and I haven't done any kind of upgrades.
The router doesn't render the components itself. I wonder if even the correct route gets rendered based on path.
Only the first route is working fine, the other two don't seem to work.
AssetPage.js
import React from "react";
import { Route } from "react-router-dom";
import AssetNavBar from "./AssetNavBar";
import "./assets.css";
function AssetPage() {
return (
<div className="top-div">
<header>
<AssetNavBar />
</header>
<main className="main-flex">
<Route path="/assets/add" exact><h1>in main</h1></Route>
<Route path="/assets/storage">
<h1>in storage</h1>
</Route>
<Route path="/assets/view">
<h1>in view</h1>
</Route>
</main>
</div>
);
}
export default AssetPage;
AssetNavBar.js
import React from "react";
import {Link} from 'react-router-dom';
function AssetNavBar() {
return (
<div>
<nav>
<ul className="ul-side-by-side">
<li>
<Link to="/assets/add">add assets</Link>
</li>
<li>
<Link to="/assets/view">view assets</Link>
</li>
<li>
<Link to="/assets/storage">storage</Link>
</li>
</ul>
</nav>
</div>
);
}
export default AssetNavBar;
index.js - [starting point]
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import {BrowserRouter} from "react-router-dom";
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
document.getElementById('root')
);
App.js
import "./App.css";
import { Route } from "react-router-dom";
import SingleItem from "./components/inventory/SingleItem"
import HomeNavBar from "./components/HomeNavBar";
import InventoryPage from "./components/inventory/InventoryPage";
import ProductPage from "./components/products/ProductPage";
import PurchasePage from "./components/purchase/PurchasePage";
import SupplierPage from "./components/supplier/SupplierPage";
import AssetPage from "./components/assetFolder/AssetPage";
function App() {
return (
<div className='horizontal-div-boxes'>
<header>
<HomeNavBar/>
</header>
<main className="main-flex">
<Route path="/dashboard">
<SingleItem />
</Route>
<Route path='/inventory'>
<InventoryPage/>
</Route>
<Route path='/purchase'>
<PurchasePage/>
</Route>
<Route path='/products/add'>
<ProductPage/>
</Route>
<Route path='/suppliers/add'>
<SupplierPage/>
</Route>
<Route path='/assets/add'>
<AssetPage/>
</Route>
</main>
</div>
);
}
export default App;
version
└─┬ react-router-dom#5.3.0
└── react-router#5.2.1
Issue
The AssetPage component is rendered on a "/assets/add" path and there are no other "/assets*" paths, so the other two links don't link to any routes that render content.
<Route path='/assets/add'>
<AssetPage />
</Route>
In other words, when you link to "/assets/view" or "/assets/storage" the path no longer matches "/assets/add" and the AssetPage component is unmounted.
Solution
I am sure that you want this root route to be just "/assets" to allow further matching any "/assets*" paths.
<Route path='/assets'>
<AssetPage />
</Route>

Component not rendering using react router

I'm using React Router with the Link tag to handle the routing between the pages on my app. Well, when I click on the home page from the landing page the URL changes but the DOM does not show any of the code that I have for the home component Also I'm not getting any errors and the home page is just blank. I have included the code for my Index.js and App.js so you can see. Just a side note I'm using react-router-dom V 5.2.0, and react-redux V 7.2.2.
Index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { BrowserRouter } from 'react-router-dom';
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>, document.getElementById('root')
);
reportWebVitals();
App.js
import React from 'react';
import { Route, BrowserRouter, Switch, withRouter} from 'react-router-dom';
//COMPONENTS IMPORTS
import LandingPage from './components/LandingPage';
import HomePage from './components/HomePage';
function App() {
return (
<div>
<BrowserRouter>
<Switch>
<Route exact path= "/" component = {LandingPage} />
<Route exact path = "/HomePage" component ={HomePage} />
</Switch>
</BrowserRouter>
</div>
)
}
export default withRouter (App);
LandingPage
//tools import
import React, { Component } from 'react'
import { Link } from 'react-router-dom';
//css Import
import '../App.css';
//Animation Import
import Anime from 'react-anime';
//Image Imports
import silentCareLogo from '../Images/silentCareLogo.png';
// React-icons//
import {BsArrowRightShort} from "react-icons/bs";
export default class landingPage extends Component {
render() {
return (
<div className="landingContainer">
<div className="logoContainer">
<Anime className = "animeDiv" opacity = {[0,1]} duration={25000}>
<img src={silentCareLogo} className="companyLogo" alt="logo" />
</Anime>
<Anime className="animeHmeBtnDiv" opacity = {[0,1]} duration={20000} delay={2000}>
<Link to="/home" className="landingHomeBtn">home <BsArrowRightShort /> </Link>
</Anime>
</div>
</div>
)
HomePage
import React, { Component } from 'react'
//css Import
import '../App.css';
//Animation Import
import Anime from 'react-anime';
export default class HomePage extends Component {
render() {
return (
<div>
<div className ="homeContainer">
<h1>home page</h1>
</div>
</div>
)
}
}
Please check you app.js You home page component is in /HomePage
and you are trying to get it in /home
function App() {
return (
<div>
<BrowserRouter>
<Switch>
<Route exact path='/' component={LandingPage} />
<Route exact path='/HomePage' component={HomePage} />
</Switch>
</BrowserRouter>
</div>
);
}
And in the Landing page, you are trying to get home page in /home path which does not exist in your app.
<Link to="/home" className="landingHomeBtn">home <BsArrowRightShort /> </Link>
whereas your component is in " /HomePage "
Solution:
change your path in App.js from /HomePage to /home
function App() {
return (
<div>
<BrowserRouter>
<Switch>
<Route exact path='/' component={LandingPage} />
<Route exact path='/home' component={HomePage} />
</Switch>
</BrowserRouter>
</div>
);
}

React Router v4 nested routes not working

I just upgraded to React Router v4 with a react 15.4.2 app based on a boilerplate from Fountain JS and after reading this article, I wrote this:
// Index.js
import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import {Route, BrowserRouter, Switch} from 'react-router-dom';
import {Main} from './app/pages/main';
import {App} from './app/pages/app';
import './index.scss';
const MainRouter = () => (
<div>
<main>
<Switch>
<Route path="/home" component={Main}/>
<Route path="/app" component={App}/>
</Switch>
</main>
</div>
);
ReactDOM.render(
<BrowserRouter>
<MainRouter/>
</BrowserRouter>,
document.getElementById('root')
);
// App.jsx
import React, {Component} from 'react';
import {Switch, Route} from 'react-router-dom';
import PropTypes from 'prop-types';
import {Dashboard} from './app/dashboard';
import {Money} from './app/money';
import {Header} from '../tpl/header';
import {Footer} from '../tpl/footer';
export class App extends Component {
render() {
return (
<div>
<Header/>
<main>
<Switch>
<Route path="/app/dashboard" exact component={Dashboard}/>
<Route path="/app/money" exact component={Money}/>
</Switch>
</main>
<Footer/>
</div>
);
}
}
// dashboard.jsx
import React, {Component} from 'react';
export class Dashboard extends Component {
render() {
return (
<div>
<h1>This is the Dashboard</h1>
</div>
);
}
}
Navigating to http://localhost:3000/app/ seems to work fine but navigating to http://localhost:3000/app/dashboard gives 404. What could I be possibly getting wrong?
PS Even on the routes that work, adding a trailing slash will not work.
You need to correct dashboard route at the end of App's render. No need for another Switch over there. So in your App.jsx file's render method :
render() {
return (
<div>
...
<Route path="/app/dashboard" component={Dashboard}/>
</div>
);
}

Render Two Components at First Run

I'm working on a small react app with four components: Main, Nav, Timer, and Countdown. When I run it (open a new tab and load the app, not refresh), I want this app to render Main and Timer components simultaneously, but the app only renders Main Component and I see a blank page with only Nav component has been rendered.
I tried using IndexRoute but I got a strange error and it didn't work for me.
Here's my code:
import React from 'react';
import ReactDOM from 'react-dom';
import {BrowserRouter as Router, Route, IndexRoute, hashHistory} from 'react-router-dom';
import Main from 'Main';
import Timer from 'Timer';
import Countdown from 'Countdown';
ReactDOM.render(
<Router history={hashHistory}>
<div>
<Route path="/" component={Main} />
<Route exact path="/timer" component={Timer} />
<Route path="/countdown" component={Countdown} />
</div>
</Router>,
document.getElementById('app')
);
Main Component:
import React, {Component} from 'react';
import Nav from 'Nav';
class Main extends Component {
render() {
return (
<Nav />
);
}
}
export default Main;
Nav Component:
import React, {Component} from 'react';
import {NavLink} from 'react-router-dom';
class Nav extends Component {
render() {
return (
<div className="top-bar">
<div className="top-bar-left">
<ul className="menu">
<li className="menu-text">React Timer</li>
<li><NavLink to="/timer" activeClassName="active-link" activeStyle={{fontWeight: 'bold'}}>Timer</NavLink></li>
<li><NavLink to="/countdown" activeClassName="active-link" activeStyle={{fontWeight: 'bold'}}>Countdown</NavLink></li>
</ul>
</div>
<div className="top-bar-right">
<ul className="menu">
<li className="menu-text">Created by Milad Fattahi</li>
</ul>
</div>
</div>
);
}
}
export default Nav;
The other two components contain only a simple text element.
IndexRoute and hashHistory are not available in react-router-dom. Make use of Switch to render only first matching Route. You can then have the Child Routes in the component itself.
Also when you use Switch , have the route with path="/" at last otherwise it will match it at the begining and will not render your other routes
You can configure your time component to be a child of the Main component like
import {BrowserRouter as Router, Route} from 'react-router-dom';
ReactDOM.render(
<Router>
<Switch>
<Route path="/" component={Main} />
</Switch>
</Router>,
document.getElementById('app')
);
Then in Main component
import React, {Component} from 'react';
import Nav from 'Nav';
import {Route, Redirect} from 'react-router-dom'
class Main extends Component {
render() {
return (
<div>
<Nav />
<Redirect to="/timer" />
<Route exact path="/timer" component={Timer} />
<Route path="/countdown" component={Countdown} />
</div>
);
}
}
export default Main;

Resources