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>
);
Related
I tried many different ways including having baseline on BrowserRouter and add line, initially I was using Switch but soon realised that npm 6.0 onwards has changed it to Routes, so I downgrade it to npm 5.2.3. However, a blank screen still persists.
Hope to get suggestions from the experts here
this is my App.js
import logo from './logo.svg';
import './App.css';
import {Home} from './Home';
import {Department} from './Department';
import {Employee} from './Employee';
import {Navigation} from './Navigation';
import { BrowserRouter, Switch, Route } from "react-router-dom";
function App() {
return (
<BrowserRouter basename='/index.html'>
<div className="container">
<h3 className="m-3 d-flex justify-content-center">
React JS Tutorial
</h3>
<Navigation/>
<Switch>
<Route path='/' component={Home} exact/>
<Route path='/department' component={Department}/>
<Route path='/employee' component={Employee}/>
</Switch>
</div>
</BrowserRouter>
);
}
export default App;
This is index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
reportWebVitals();
I remember running into this issue, try the code, it might not work but I think you can't have any other component within BrowserRouter, hopefully that gives you at least a direction.
function App() {
return (
<BrowserRouter basename='/index.html'>
<Switch>
<Route path='/' component={Home} exact/>
<Route path='/department' component={Department}/>
<Route path='/employee' component={Employee}/>
</Switch>
</BrowserRouter>
);
}
I have a navbar that is rendered in every route while the route changes on click.
./components/navbar.jsx
import React, { Component } from 'react';
import '../App.css';
import { Link } from 'react-router-dom';
class Navbar extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<div id = 'navbar'>
<div className='name-head'>
My Name
</div>
<div id = 'nav-links-container'>
<Link to='/experiences'>
<div className = 'nav-links'>
Experiences
</div>
</Link>
<div className = 'nav-links'>
Projects
</div>
<div className = 'nav-links'>
Skills
</div>
<div className = 'nav-links'>
Resume
</div>
</div>
</div>
);
}
}
export default Navbar;
./components/experiences.jsx
import React, { Component } from 'react';
class Experiences extends Component {
render() {
return (
<div>
<h1>hi</h1>
</div>
);
}
}
export default Experiences;
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import reportWebVitals from './reportWebVitals';
import Navbar from './components/Navbar';
import Home from './components/Home';
import Experiences from './components/experience';
import {
BrowserRouter as Router,
Routes,
Route
} from 'react-router-dom';
ReactDOM.render(
<React.StrictMode>
<Navbar />
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/experiences" element={<Experiences />} />
</Routes>
</Router>
</React.StrictMode>,
document.getElementById('root')
);
reportWebVitals();
The error doesn't come when I remove the <Link> from the experiences tag in navbar.
There is a similar question posted here: Error: useHref() may be used only in the context of a <Router> component
but doesn't help.
I'm using react router v6
Issue
You are rendering the navbar outside the routing context. The Router isn't aware of what routes the links are attempting to link to that it is managing. The reason routing works when directly navigating to "/experiences" is because the Router is aware of the URL when the app mounts.
<Navbar /> // <-- outside router!!
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/experiences" element={<Experiences />} />
</Routes>
</Router>
Solution
Move it inside the routing context so the Router is aware and can manage routing correctly.
<Router>
<Navbar />
<Routes>
<Route path="/" element={<Home />} />
<Route path="/experiences" element={<Experiences />} />
</Routes>
</Router>
react-router-dom#6.4 Data APIs
If you are using the new Data routers you can hit this issue if you attempt to render a header/navbar outside the RouterProvider component. For this you can create a layout route that is part of the routing configuration passed to createBrowserRouter (and other variants).
Example:
const AppLayout = () => (
<>
<Navbar />
<Outlet />
</>
);
const router = createBrowserRouter(
createRoutesFromElements(
<Route element={<AppLayout />}>
<Route path="/" element={<Home />} />
<Route path="/experiences" element={<Experiences />} />
</Route>
)
);
...
<RouterProvider router={router} />
in React Route v6 you can solve this giving the route context to your entire App with <BrowserRouter>
This is an complete example of index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import { App } from './components/App/App.jsx';
ReactDOM.render(
<React.StrictMode>
<BrowserRouter> //that is the key
<App />
</BrowserRouter>
</React.StrictMode>,
document.getElementById('root')
);
If you are still having problem with this one, it is because react-router-dom relies on React context to work when you try to unit-test it. This makes <Link /> or <Route /> obsolete.
Try reading the react-router-dom documentation.
Instead of using
render(<Example />)
that have either or inside it, you can try
render(<MemoryRouter>
<Example />
</MemoryRouter>)
Hope this solves your problem
In react-router-dom:6.x and react:18.x, we should use the Router in the following way to resolve the issue:
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router } from 'react-router-dom';
import { App } from './App.js';
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<Router> // Router
<App /> // Application
</Router>
</React.StrictMode>
);
Links are inside Navbar &
Navbar is outside of Router
=> links are outside of Router => Router will not manage Links
Solution
Move the Navbar into Router section. Example:
<Router>
<Navbar /> // <===========
<Routes>
<Route />
<Route />
</Routes>
</Router>
Wrap navbar with BrowserRouter
import { BrowserRouter, Routes, Route } from "react-router-dom";
<BrowserRouter>
<AppNavBar />
<Routes>
<Route path="/" element={<Home />} />
<Route path="/wall" element={<WallPost />} />
</Routes>
</BrowserRouter>
Install in your project React Router v6.
npm install react-router-dom#6
Then use BrowserRouter in your index.js file, below like this:
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import { BrowserRouter } from 'react-router-dom';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>
);
React Router v6 this problem common one, you can simply replace "index.js" code. you will got solution-
import React from 'react';
import { createRoot } from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { BrowserRouter } from 'react-router-dom';
const container = document.getElementById('root');
const root = createRoot(container);
root.render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>
);
This error happens because Link component needs to reach out to react-router context object. Your Navbar component is using Link component but Navbar is not wrapped by router context.
A similar error happens in a testing environment. If you have a component that uses Link component and if you render this component in a testing environment, you will get the same error because Link component needs to access a router context. In the case of test environment:
import { render } from "#testing-library/react";
// this will throw same error
test("testing", () => {
render(<ComponentRendersLink/>);
});
Solution in this case to wrap it with a router
// there are more router options
import { MemoryRouter } from "react-router-dom";
render(
<MemoryRouter>
<ComponentRendersLink />
</MemoryRouter>
);
This is very much an edge case, but in my case it turned out a lib I develop had react-router-dom: 6.0.2 installed and project that uses it v6.3.0
Your links just needs to be within a BrowserRouter component since you use v6
After importing
<BrowserRouter>
<Link to='page'>
</BrowserRouter>
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>
);
}
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>
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>
);
}