175

Matched leaf route at location "/" does not have an element. This means it will render an with a null value by default resulting in an "empty" page

//App.js File

import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import Home from './pages/Home';
// import ReactDOM from "react-dom";


const App = () => {
  return (

    <Router >
      <Routes>

        <Route path="/" component={ Home }></Route>
      </Routes>
    </Router>


  )
}

export default App;

My any react router related code not working i don't know why it happend when i start insert some route in program so it show this error

Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
user16102215
  • 1,751
  • 2
  • 5
  • 4
  • 1
    Adding a couple of sources that helped me: [React Router v6](https://remix.run/blog/react-router-v6) and the corresponding [GitHub thread](https://github.com/remix-run/react-router/issues/8146#issuecomment-947860640) – willwrighteng Nov 24 '22 at 08:48

8 Answers8

354

In V6, you can't use the component prop anymore. It was replaced in favor of element:

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

More info in the migration doc.

tavoyne
  • 5,591
  • 2
  • 14
  • 24
69

I had the same problem. Replace component with element and it worked.

Replace this:

<Route path="/" component={HomePage} exact />

with this:

<Route path="/" element={<HomePage/>} exact />
Dara Java
  • 2,410
  • 3
  • 27
  • 52
Anita Jayana
  • 771
  • 6
  • 3
  • 4
    I want to pass a data to a function inside ``. How to do it? I tried, ` ( )}/>` but the page became blank with no errors but warning as, `Matched leaf route at location "/" does not have an element. This means it will render an with a null value by default resulting in an "empty" page.` – Common Man Aug 16 '22 at 10:50
  • 1
    that's what I am trying to do but the data is not showing, only a blank screen. – Alex Antoine Jan 11 '23 at 18:13
  • 1
    @AlexAntoine & CommonMan either of you figure out how to do this.. I am trying to do the same thing with the render attribute. – Flak DiNenno Jan 22 '23 at 14:54
26

I had the same error however my fix was slightly different I had spelled element wrong.

<Route exact path='/MyGames' element={<MyGames/>}/>

and this was the error it gave me in the browser console

Matched leaf route at location "/MyGames" does not have an element. This means it will render an <Outlet /> with a null value by default resulting in an "empty" page.

Kalnode
  • 9,386
  • 3
  • 34
  • 62
KathyB
  • 261
  • 2
  • 3
  • You right, in current version component doesn't work, I used element and work for me – anderson j mariño o. Dec 21 '21 at 21:18
  • Thank you! My first reaction was "how can this be a solution, if you mistype 'element' than React should give you a proper error". But when I check my code i typed "elemenet" =) Thank you! – Buğra Otken Jun 03 '22 at 11:31
21

Very simple:

  1. use element instead of component
  2. wrap the your component like this: {<Home/>} instead of {Home}
<Route path="/" component={ <Home/> } />
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Adioz Daniel
  • 339
  • 1
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 01 '22 at 09:42
  • Hmm... what is the point of this change? Just to make it more explicit? Also as of version 6, you can't use component anymore, now you have to use `element`. – Longblog Mar 02 '22 at 03:45
  • Accept this answer :) – Michael Wegter Aug 20 '23 at 18:06
9

in version 6:

component replaced with element and needs to close "</Route>"

 <Route exact path="/" element={<AddTutorial />}></Route>

https://reactrouter.com/docs/en/v6/getting-started/overview

3

Instead of

<Route path='/about' component={About} />

I used

<Route path='/about' element={<About />} />

to resolve my problem and now working fine.

2

This is a common problem if you are using react-router-dom V6 in your react app. To solve this it's simple

In your code Replace component with element Replace {home} with {<home/>}

This becomes... <Route path="/" element={<Home/>}></Route>

This will definitely solve the problem.

Phestus
  • 31
  • 3
0

If you're using react-router-dom 6 or above, you may have a routes array that includes parent and child routes. You may then try to open a route such as

/portal

and get this error because that component corresponds to a child route

/:customerid/portal

but you haven't read your routes (and their child routes) closely enough to see that.

David
  • 895
  • 8
  • 12