Introduction to Active-Route
The modern web application landscape necessitates highly dynamic and seamless user navigation. Active-route
is a powerful tool designed to manage routes efficiently in web applications, making seamless navigation a reality. This tutorial dives deep into the active-route
library, providing you with an extensive guide on its APIs and practical code snippets.
Core APIs of Active-Route
1. activateRoute
This API activates a specific route in the application.
activateRoute('/home');
2. deactivateRoute
Deactivates a specific route, effectively removing it from the active routes.
deactivateRoute('/home');
3. getCurrentRoute
Returns the current active route.
const currentRoute = getCurrentRoute();
console.log(currentRoute); // '/home'
4. listActiveRoutes
Provides the list of all active routes currently in use.
const activeRoutes = listActiveRoutes();
console.log(activeRoutes); // ['/home', '/dashboard']
5. isRouteActive
Checks if a particular route is currently active.
const isActive = isRouteActive('/dashboard');
console.log(isActive); // true
6. refreshRoute
Refreshes the given route.
refreshRoute('/profile');
7. navigateTo
Programmatically navigates to a specified route.
navigateTo('/settings');
8. onRouteChange
Listener that triggers a callback function when the route changes.
onRouteChange((newRoute) => {
console.log('Route changed to:', newRoute);
});
App Example Using Active-Route
Below is an example of a single-page application utilizing active-route
:
import { activateRoute, getCurrentRoute, navigateTo, onRouteChange } from 'active-route';
function App() {
// Set up initial route
activateRoute('/home');
// Route change listener
onRouteChange((newRoute) => {
console.log('Navigated to:', newRoute);
});
// Navigation function
function goToProfile() {
navigateTo('/profile');
}
// UI Rendering logic
return (
<div>
<h1>Current Route: {getCurrentRoute()}</h1>
<button onClick={goToProfile}>Go to Profile</button>
</div>
);
}
By leveraging active-route
in your application, you can ensure efficient and smooth page transitions while maintaining a highly modular codebase. Optimize your application’s navigation from today!
Hash: 9079a31e4df6290c8d88144dad0c66d1f95f6f74175c50305643fdb9c0e07034