Home > Mobile >  React router v6 experiencing blank page on routes
React router v6 experiencing blank page on routes

Time:06-15

Updated from "react-router-dom": "^5.1.2" to "react-router-dom": "^6.3.0"
The app has a few Context.Providers just outside BrowserRouter, a 404 route, and a few implicit nested routes for a few pages (denoted by path not exact path)

code:

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';
import { BrowserRouter, Routes, Route } from "react-router-dom";

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

./components/App.js

import React, { useState } from "react";
import { BrowserRouter, Route, Routes } from "react-router-dom";
import TierA from "./TierA"
import TierB from "./TierB"
import Catalogue from "./Catalogue"
import Admin from "./Admin"
import NotFound from "./NotFound" // this is the 404 page component

import PlayerContext from "./Player/context"
import SearchContext from "./Search/context"
import { useSearch } from "../hooks/useSearch"


const App = () => {

    const searchText = useSearch()
    const [playerItem, setPlayerItem] = useState(null)

    return (
        <React.Fragment>
        <SearchContext.Provider value={searchText}>
        <PlayerContext.Provider value={{ playerItem, setPlayerItem }}>
        <BrowserRouter basename={process.env.PUBLIC_URL}>
                <Routes>
                    <Route element={TierA} exact path="/a_tier" />
                    <Route element={TierB} exact path="/b_tier" />
                    <Route element={Catalogue} path="/catalogue" />
                    <Route element={Admin} path="/admin" />
                    <Route element={NotFound} path="*" /> 
                </Routes>
            </BrowserRouter>
        </PlayerContext.Provider>
        </SearchContext.Provider>
        </React.Fragment>
    )
}


export default App;

Before updating to react-router-dom v6, the old version used to render the 404 page when hitting the base / route and the named routes rendered correctly.
Now, hitting any of the routes above shows a blank page, no warnings or errors stand out in console Please advise, thanks in advance

runs with npm start

"scripts": {
    "start": "react-scripts start",
...

CodePudding user response:

In react-router-dom@6 the Route component API/syntax changed significantly. The element prop takes a ReactNode, a.k.a. JSX. Pass the React components as JSX.

Example:

<BrowserRouter basename={process.env.PUBLIC_URL}>
  <Routes>
    <Route element={<TierA />} path="/a_tier" />
    <Route element={<TierB />} path="/b_tier" />
    <Route element={<Catalogue />} path="/catalogue" />
    <Route element={<Admin />} path="/admin" />
    <Route element={<NotFound />} path="*" /> 
  </Routes>
</BrowserRouter>
  • Related