react-router-dom v6 Routes showing blank page
In react-router-dom@6
the Route
components don't render routed content as children, they use the element
prop. Other Route
components are the only valid children of a Route
in the case of building nested routes.
export default function WebRoutes() {
return (
<Routes>
<Route path='/' element={<Welcome />} />
</Routes>
);
}
Ensure that you have rendered a router around your app.
import { BrowserRouter as Router } from 'react-router-dom';
ReactDOM.render(
<React.StrictMode>
<Router>
<WebRoutes />
</Router>
</React.StrictMode>,
document.getElementById('root')
);
React Router Dom returns a blank page when going to a particular route
You can not access data
directly like this. If you set a state property named data
, you have to access it using this.state.data
in the render()
function.
this.state.data.map(item => {
...
});
Another issue you might face is that the componentDidMount
function is triggered after a first render of the component (see React componentDidMount()) which means that you will access data in render()
before it is created and use map
on an undefined value.
I suggest you either use componentWillMount()
or create the data property in a constructor
like so:
constructor(props) {
super(props);
this.state = { data: [] };
}
React-router-dom showing blank screen
This is a react@18
change in how React apps are rendered. createRoot
takes a DOMNode reference, not JSX. Once the root is created, then you can call a render
method on it.
Example:
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App";
const rootElement = document.getElementById("root");
const root = createRoot(rootElement);
root.render(
<StrictMode>
<App />
</StrictMode>
);
See react-dom-client
for more in-depth detail.
React router v6 experiencing blank page on routes
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>
React Router Dom routes are returning blank pages
I assume you are using React Router Dom v6
since you are using Routes
instead of Switch
, in which case it should be element
not component
the propriety where you pass that component for that route. Also call the component when passing it, like so:
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import Sidebar from "./components/Sidebar";
import Dash from "./pages/Dash";
import Prof from "./pages/Prof";
function App() {
return (
<Router>
<Sidebar />
<Routes>
<Route path="/" exact element={<Dash />} />
<Route path="/profile" exact element={<Prof />} />
</Routes>
</Router>
);
}
export default App;
React Route shows blank page
Route can only be child of Routes. Try adding Routes as parent like below and you should see WelcomeComponent loading up.
render() {
return (
<div className="TodoApp">
<Router>
<Routes>
<Route path="/" element={<WelcomeComponent />} />
</Routes>
</Router>
</div>
);
}
Getting blank page on using Route | React
import './App.css';
import Navbar from './components/Navbar';
import HomeScreen from './screens/HomeScreen'
mport { BrowserRouter, Routes, Route} from 'react-router-dom'
function App() {
return (
<div className="App">
<Navbar/>
<BrowserRouter>
<Routes>
<Route path='/home' element={<HomeScreen/>} />
</Routes>
</BrowserRouter>
</div>
);
}
export default App;
The bit that wasnt working was where you defined your element so change
<Route path='/home' exact component={HomeScreen} />
to this
<Route path='/home' element={<HomeScreen/>} />
Related Topics
Javascript Generate Video Thumbnail from Video Url
How to Trigger a Click Event on Disabled Elements
Validate Phone Number With Yup
Angular [Innerhtml]: How to Render Additional Elements Inside an Element With [Innerhtml] Directive
Chat Box, Auto Scroll to Bottom
Textarea With Initial Auto Height by Content With Pure CSS
Rendering Empty Space in React
How Insert JavaScript into Vue File
How to Run a Program or Batch File on the Client Side
Firebase.Firestore() Is Not a Function When Trying to Initialize Cloud Firestore
Html5 and JavaScript to Play Videos Only When Visible
Change Specific Button Text on Click Inside Ngfor
Jquery Click Events Firing Multiple Times
Size of Json Object (In Kbs/Mbs)
How to Get the Id of a Button That Was Clicked - Reactjs
Possible to Detect If a User Has Multiple Tabs of Your Site Open
Onclick Table Row With Onclick Button and Bootstrap Modal Inside