Appearance
How to create a new page β
To create a new route, simply add a file to the src/pages
directory, then navigate to src/routes/Router.jsx
to define your new route.
Creating a new page /contact-us β
To create a new page, navigate to the
src/pages
directory and create your new file:src/pages/contact-us.jsx
jsxconst ContactUs = () => { return <h1>Contact us</h1>; }; export default ContactUs;
Navigate to the
src/routes/Router.jsx
file and add your new page route:jsxconst routes = [ { path: "contact-us", element: <ContactUs />, } ]
/contact-us
route will be created.
TIP
We recommend using React Router's lazy loading feature to minimize bundle size, resulting in faster load times and enhanced performance.
Dynamic URL /users/<id> β
Dynamic routes are represented by square brackets. Directories and pages can be dynamic.
Creating dynamic routes is a breeze! π
For example, to develop a dynamic route like /products/<id>
, where id can be any integer, simply follow these steps:
Create a new directory called products within the
src/pages
directory, and then add a new file named id inside that directory.π pages βββ Products.jsx
Place the following content inside the
Products/Router.jsx
file:jsxconst Products = () => { const { id } = useParams(); return ( <div> <h1>Products</h1> <p> product id: {id}</p> </div> ); }; export default Products;
Place the following content inside the
src/routes/Products.jsx
file:jsxconst routes = [ { path: "products/:id", element: <Products />, } /* ... */ ]
Now, if you navigate to /products/1
it will render product id: 1
.
Note that the :
in the :id
URL segment means Dynamic Segment.
404 page - Catching all routes β
We provide users with a customizable notice when they enter an incorrect URL, such as a 404 page, ensuring a user-friendly experience.
jsx
const routes = [
{
path: "*",
element: <NotFound/>,
},
/* ... */
]
The content in src/pages/Errors/404.jsx
file will be presented when you visit any URL you haven't built.
Using different layout β
By default, the pages you create will be displayed in the Default layout (src/layouts/Default.jsx), which features a header and vertical navigation. However, you can opt for another predefined or custom layout from this directory.
To keep application bundles small and support code-splitting for our routes, we use React Router lazy loading for components.
To do this, Update your file as follows:
js
const routes = [
{
element: <DefaultLayout/>,
children: [
{
path: "home",
lazy: () => import('./YourComponent'), // lazy loaded component
}
]
},
{
element: <Error/>,
children: [
{
path: "400",
element: <BadRequest/>,
},
{
path: "403",
element: <Forbidden/>,
}
]
}
]
Route Handles β
Route handles in React Router simplify sharing data and functions across nested routes, improving code organization and flexibility.
js
const routes = [
{
path: "yourPath",
lazy: () => import('./Component'), // lazy loaded component
handle: {
noBackToTop: Boolean, // Determines whether the 'Back to Top' button should be displayed on the page
fixedHeight: Boolean, // Applies fixed-height styles to the page content, such as in email and chat pages.
excludeAnimation: Boolean, // Checks whether the route should include transition animations.
navActiveLink: String, // navActiveLink is an attribute that keeps a navigation link active for related sub-pages.
// For example, when youβre on the TicketDetails page at (/ticket/:id), the ticket nav item (/ticket) remains active in the sidebar, showing that youβre still within the Ticket section.
}
}
]