create-react-app-blog icon indicating copy to clipboard operation
create-react-app-blog copied to clipboard

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>.

Open stephenhmarsh opened this issue 3 years ago • 3 comments

I followed the tutorial but the code in this project is 2 years old and doesn't seem to work with newer installations of react-router-dom.

Given the current App.js and "react-router-dom": "^6.3.0", I get this error:

[Error] 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>.
	performReactRefresh (bundle.js:46405)
	(anonymous function) (bundle.js:761)

stephenhmarsh avatar Jul 06 '22 16:07 stephenhmarsh

Downgrading to react-router-dom@5 prevents this error, for anyone following the tutorial in 2022.

stephenhmarsh avatar Jul 09 '22 00:07 stephenhmarsh

Downgrading to react-router-dom@5 prevents this error, for anyone following the tutorial in 2022.

Nice one.

code-hyper avatar Jul 15 '22 10:07 code-hyper

Downgrading to react-router-dom@5 prevents this error, for anyone following the tutorial in 2022.

Nice one.

There's no reason to downgrade. You can use React Router Dom version 6.4.1 with these changes:

<BrowserRouter>
    <Routes>
        <Route  path="/" exact element={<AllPosts />} />
        <Route path="/:slug" element={<OnePost />}  />
    </Routes>
</BrowserRouter>

Essentially you're swapping the <div> for <Routes> and the <Route> changes from:

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

to:

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

and the second route of:

<Route component={OnePost} path="/:slug" />

changes to:

<Route path="/:slug" element={<OnePost />}  />

codingChewie avatar Sep 23 '22 20:09 codingChewie