Why is React-Router not rendering a route when I add its path to the end of the localhost:3000 URL?

I’ve been working through this React tutorial and have gotten stuck. (Chapter 16: React Router). My basic boilerplate html components all render properly in App.js UNTIL I wrapped some inside <Routes> tags and added the path="" and element={} attributes. The code below of my App.js file is where I’m at (05:00:38 in the tutorial video)

Note: You’ll notice right away that I’ve already addressed problems and changed some code to overcome discrepancies between the tutorial video (which was made in 2021) and changes made in 2022 to React Router 6. These revisions included changing components to elements, Switch to Routes, etc. In retrospect I realize now I should have picked a more recent tutorial but here we are…

import Header from './Header';
import Nav from './Nav';
import Footer from './Footer';
import Home from './Home';
import NewPost from './NewPost';
import PostPage from './PostPage';
import About from './About';
import Missing from './Missing';
import { Router, Route, Routes, useHistory } from 'react-router-dom';
import { useState, useEffect } from 'react';

function App() {
  return (
    <div className="App">
      <Header />
      <Nav />
      <Routes>
        <Route path="" element={<Home />} />
        <Route path="post" element={<NewPost />} />
        <Route path="post/:id" element={<PostPage />} />
        <Route path="about" element={<About />} />
        <Route path="*" element={<Missing />} />
      </Routes>
      <Footer />
    </div>
  );
}

export default App;

Everything seems fine, i.e. only Header, Nav, Home, and Footer are rendering on "localhost:3000".

Screenshot of localhost:3000

The problem is when I go to "localhost:3000/post: to test and see if Home gets swapped out and re-rendered as NewPost per the tutorial. Instead, I get a blank screen. The same blank screen occurs when I try to navigate to "localhost:3000/post/:1" or "localhost:3000/about".

I suspect the problem lies somewhere in the App.js file above, my index.js file (below), or my NewPost.js file (below that):

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';

ReactDOM
  .createRoot(document.getElementById('root'))
  .render(
    <Router>
      <Routes>
        <Route path="/" element={<App />} />
      </Routes>  
    </Router>
  );
import React from 'react'

const NewPost = () => {
  return (
    <main>
      <h1>NewPost</h1>
    </main>
  )
}

export default NewPost

I’ve searched Stack Overflow and found a few similar problems, mainly: DOM does not render when adding a nested route in react router. Why would this be happening? and react-router-dom <routes> not rendering anything in localhost:3000. Unfortunately I haven’t yet found a fix. Several people suggested removing the exact keyword, which I did since it’s since been removed/deprecated, but it didn’t help. (Those previous issues ended up being resolved by making the React Router 6 syntax updates I noted at the beginning of my post.)

I’ve uploaded my full codebase here in case it’s helpful: https://github.com/pudgyturtle/react-blog I can’t for the life of me figure out what the problem is.

New contributor

pudgyturtle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

In React-Router-DOM 6 all routes are exactly matched by path, and the root "/" route rendering App doesn’t allow for matching descendent routes.

You can resolve this by appending the wildcard/splat "*" matcher to the root route:

<Route path="/*" element={<App />} />

Which allows the descendent routes to also be matched.

That said, this root route isn’t useful at all, you should just render App directly under the BrowserRouter and then the Routes and Route components rendered by App will work as root-level routes.

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import { BrowserRouter as Router } from 'react-router-dom';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <Router>
    <App />
  </Router>
);
function App() {
  return (
    <div className="App">
      <Header />
      <Nav />
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="post" element={<NewPost />} />
        <Route path="post/:id" element={<PostPage />} />
        <Route path="about" element={<About />} />
        <Route path="*" element={<Missing />} />
      </Routes>
      <Footer />
    </div>
  );
}

3

At your index.tsx file it should look like this:

ReactDOM.createRoot(
  document.getElementById('root'))
  .render(<App />);

and in your App.tsx it should look like this:

import Header from './Header';
import Nav from './Nav';
import Footer from './Footer';
import Home from './Home';
import NewPost from './NewPost';
import PostPage from './PostPage';
import About from './About';
import Missing from './Missing';
import { BrowswerRouter as Router, Route, Routes, useHistory } from 'react-router-dom';
import { useState, useEffect } from 'react';

function App() {
  return (
    <div className="App">
      <Header />
      <Nav />
      <Router>
        <Routes>
          <Route path="" element={<Home />} />
          <Route path="post" element={<NewPost />} />
          <Route path="post/:id" element={<PostPage />} />
          <Route path="about" element={<About />} />
          <Route path="*" element={<Missing />} />
        </Routes>
      </Router>
      <Footer />
    </div>
  );
}

Notice that I imported BrowserRouter as Router from react-router-dom

It looks like there’s a small issue with how your routes are set up in your React app. That’s why you’re seeing a blank screen when you try to go to certain pages.

Change the Route Path in App.js:

You have this line in your App.js:

<Route path="" element={<Home />} />

The path is empty, which can cause issues. Change it to / like this:

 <Route path="/" element={<Home />} />

In your index.js, you’re wrapping your App component with both Routes and Route. You don’t need that because you’ve already set up the routes in App.js. So, just wrap App with Router like this:

ReactDOM.createRoot(
  document.getElementById('root'))
  .render(
    <Router>
      <App />
    </Router>
);

If you’re still having issues,look the console in your browser o They might tell you what’s going wrong.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật