import React from "react";
import "./App.css";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Home from "./Home";
import Header from "./Header";
function App() {
return (
<div className="app">
<Router>
<Routes>
<Route path="/">
<div>
<Header />
<Home />
</div>
</Route>
</Routes>
</Router>
</div>
);
}
export default App;
I want to render both the Header and Home components one below other when I try to open the default path.
2
Answers
The reason why you are getting a blank white page when you open the default path is that the div element that is wrapping the Header and Home components is not being rendered. This is because the Routes component only renders the child component that matches the current path. In this case, the current path is /, so the Home component is rendered, but the Header component is not.
To fix this, you need to move the div element outside of the Routes component. This way, it will be rendered regardless of the current path. The updated code would look like this:
Use code with caution. Learn more
Now, when you open the default path, both the Header and Home components will be rendered one below the other.
Here is a breakdown of what is happening in the updated code:
The div element is now outside of the Routes component. This means that it will be rendered regardless of the current path.
The Home component is still rendered inside of the Routes component because it is the child component that matches the current path.