-
How can i modify my app.js file to hide Navbar for signin and signup pages, but render for other pages?
-
I want to render navbar once but screencomponents inside it should change. In navbar , I have to pass screenComponent prop in order to show content inside nav and sidebar. The code is working perfect but I don’t know whether I wrote the code perfect? what is the standard way to do this task ?
function App() {
return (<Navbar screenComponent={
<Routes> {/* User Profiling Routes */} <Route path="/" element={<Signin />} /> <Route path="signup" element={<Signup />} /> <Route path="forgotpassword" element={<ForgotPassword />} /> <Route path="emailsent" element={<EmailSent />} /> <Route path="/profile" element={<PrivateRoute Component={ProfileHome} />} /> <Route path="/wish" element={<PrivateRoute Component={Wishlist} />} /> <Route path="/add_address" element={<PrivateRoute Component={AddAddress} />} /> <Route path="/edit_address/:id" element={<PrivateRoute Component={EditAddress} />} /> <Route path="/add_payment" element={<PrivateRoute Component={AddPayment} />} /> <Route path="/edit_payment/:id" element={<PrivateRoute Component={EditPayment} />} /> <Route path="/delete_account" element={<PrivateRoute Component={DeleteAccount} />} /> <Route path="/edit_prescription" element={<PrivateRoute Component={EditPrescriptions} />} /> <Route path="/prescription_details" element={<PrivateRoute Component={PrescriptionDetails} />} /> <Route path="/add_prescription" element={<PrivateRoute Component={AddPrescription} />} /> <Route path="/change_password" element={<PrivateRoute Component={ChangePassword} />} /> <Route path="/upload_tryon_images" element={<PrivateRoute Component={UploadTryonImages} />} /> <Route path="/upload_user_image" element={<PrivateRoute Component={UploadUserImage} />} /> <Route path="/giftcards" element={<PrivateRoute Component={GiftCards} />} /> <Route path="/my_details" element={<PrivateRoute Component={MyDetails} />} /> {/* order management routes */} <Route path="/select_prescription_type" element={<PrivateRoute Component={SelectLensType} />} /> <Route path="/select_prescription_Option" element={<PrivateRoute Component={SelectPrescriptionOption} />} /> <Route path="/select_prescription_Type" element={<PrivateRoute Component={SelectPrescriptionType} />} /> <Route path="/enter_prescription" element={<PrivateRoute Component={EnterPrescription} />} /> {/* no page found */} <Route path="*" element={<NoPage />} /> </Routes> } /> </Router>
);
}export default App;
2
Answers
To hide the Navbar for the signin and signup pages and render it for other pages, you can modify your
App.js
file as follows:In this code, we use the
Router
component fromreact-router-dom
to handle routing. We define two types of routes: public routes (signin and signup) and private routes (all other routes). For the public routes, we directly render the corresponding components (<Signin />
and<Signup />
).For the private routes, we wrap them inside a
div
element that contains theNavbar
component and the nestedRoutes
component. This structure ensures that theNavbar
is rendered only once, and the content inside it (theRoutes
component) changes based on the current route.You can add additional private routes inside the nested
Routes
component as needed, replacing<Home />
with the appropriate components for each route.Make sure to import the necessary components (
Navbar
,Signin
,Signup
, etc.) in yourApp.js
file or adjust the import statements accordingly.Note: This example assumes you are using the
react-router-dom
package for routing.While Saunak’s answer should suffice, It would be good to separate your content in a protected page and public page and render the navbar conditionally based on user’s authenticated state.