I need to change the value in the header depending on which route I am in. The only dubious idea comes to place a separate header for each router, but it seems like it won’t be quite right.
<Header
loggedIn={loggedIn}
email={email}
onLoggin={setLoggedIn}
/>
<Routes>
<Route
path="/"
element={loggedIn
? <Navigate to="/main" replace />
: <Navigate to="/sign-in" replace />
}
/>
<Route path="/main" element={<ProtectedRoute element={Main}/>} />
<Route path="/sign-up" element={<Register />} />
<Route path="/sign-in" element={<Login />} />
</Routes>
I stopped at the fact that I was able to add and hide a login depending on the login to the account.
2
Answers
You can do it by taking the pathname from the URL, Like if pathname is login then show login in header, if its register show register or if user is logged in show user email. Here is a snippet of code. t solves your problem.
Here first use useLocation hook to get the pathname, then match the pathname with the paths defined in obj, Inside useEffect you can add another condition like if no path matched show user email or any dummy data.
For patterns like this I suggest creating a header layout component that keeps the value you want to render in state and exposes an updater function that routed components can update when their route is matched.
Example:
The
Header
component is updated to read the value from the provided outlet context provider.Routes
The
HeaderWrapper
wrapper component is optional since the routed components could just as easily use theuseOutletContext
hook themselves and callsetTitle
, but this couples the UI components to an implementation detail they don’t necessarily need to be concerned with.If this is the route your prefer then I’d suggest a custom hook.
Then the routed components just call the hook and pass their header title value.