First, you need to install the react-router-dom package. You can do this by running the following command in your terminal:
npm install react-router-dom
Next, you need to import the necessary components from the react-router-dom package. You can do this by adding the following lines of code to your App.js file:
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
Now, you can use the `Router`, `Routes`, and `Route` components to define the routes for your application. You can do this by adding the following lines of code to your App.js file:
function App() {
return (
<Router>
<Routes>
<Route exact path="/" component={Home} />
<Route path="/login" component={Login} />
<Route path="/protected" component={Protected} />
</Routes>
</Router>
);
}
In the above code, the `Routes` component is used to render the first `Route` that matches the current URL. The `Route` components define the routes for your application. The `exact` attribute is used to ensure that the `Route` only matches when the current URL exactly matches the specified path.
Now, you can use the `useLocation` hook from the react-router-dom package to access the current location in your components. You can do this by adding the following lines of code to your components:
import { useLocation } from 'react-router-dom';
function Header() {
const location = useLocation();
if (location.pathname === '/login') {
return null;
}
// ... rest of the code
}
function Footer() {
const location = useLocation();
if (location.pathname === '/login') {
return null;
}
// ... rest of the code
}
In the above code, the `useLocation` hook is used to access the current location. The `location.pathname` property is used to check the current path. If the current path is ‘/login’, the `Header` and `Footer` components will not be rendered.
Finally, you can use the `Header`, `Footer`, and other components in your application as needed. By following these steps, you can hide the header and footer on the login page while showing them on other pages.
Please note that this is a basic example, and you may need to adjust the code based on your specific requirements and the structure of your application.