Route Structure and Adding New Public and Private Routes
This document explains the structure of your React application's routes and guides you on adding new public and private routes.
Overall Structure:
AppRoutes
component manages all routing within the application.- Protected Routes:
- Wrapped with
AuthenticateGuard
, requiring user authentication. - Nested within the
Dashboard
layout. - Examples:
Home
,MyAccount
,Blogs
, etc.
- Wrapped with
- Unprotected Routes:
- Wrapped with
UnAuthenticateGuard
, allowing access to unauthenticated users. - Nested within the
Auth
layout. - Examples:
Login
,Register
,ForgotPassword
, etc.
- Wrapped with
- Shared Route:
NotFoundPage
accessible regardless of authentication status.
Adding New Routes:
Public Route:
- Create the page component: In the
pages
folder, create a React component for your new public page (e.g.,NewPublicPage.jsx
). - Define the route: In
AppRoutes
, add a new route outside the guards, referencing your new page component and path (e.g.,<Route path="/new-public-page" element={<NewPublicPage />} />
). - Optional Layout: If desired, consider creating a new layout component (e.g.,
PublicLayout.jsx
) and nest your new route within it for consistent styling.
Private Route:
- Follow steps 1 and 2 for Public Route.
- Wrap the route with
AuthenticateGuard
: Update the route withinAuthenticateGuard
to protect your new page (e.g.,<Route path="/new-private-page" element={<NewPrivatePage />} />
). - Update layout: Assign the
Dashboard
layout to the new route withinAuthenticateGuard
(e.g.,<Route element={<Dashboard />}>...
).
Tips:
- Use descriptive route names and paths for clarity.
- Consider nesting related routes under dedicated layouts for organization.
- Leverage
appRoutes
constants for centralized route definitions. - Remember to import necessary components and layouts in
AppRoutes
.
Example:
To add a new public page called "About Us" and a private page called "Profile":
Create
AboutUs.jsx
inpages
andProfile.jsx
inpages
.Add routes in
AppRoutes
:jsx<Route path="/about-us" element={<AboutUs />} /> <Route element={<AuthenticateGuard />}> <Route element={<Dashboard />}> <Route path="/profile" element={<Profile />} /> </Route> </Route>
By following these steps and considering the tips, you can easily add new public and private routes to your React application while maintaining a well-structured and manageable route configuration.