A lightweight, modern, and powerful React routing library built from scratch. Create single-page applications (SPAs) with declarative routing, dynamic parameters, lazy loading, and full browser history support.
π View Full Documentation | Live Examples | API Reference
- π― Declarative Routing - Define routes with intuitive
<Router>,<Route>, and<Link>components - π Dynamic Parameters - Support for route parameters using
path-to-regexp - β‘ Lazy Loading - Code-split your routes for optimal performance
- π Browser History - Full support for browser back/forward navigation
- π¨ 404 Handling - Built-in support for default/fallback routes
- π‘οΈ Error Boundaries - Graceful error handling with
RouterErrorBoundary - π§ͺ Well Tested - 28 comprehensive tests with Vitest and Testing Library
- π¦ Lightweight - Only 5.9 kB (gzipped) with minimal dependencies
- π§ TypeScript Support - Full TypeScript definitions included
- π JSDoc Comments - Complete inline documentation
- π Production Ready - Battle-tested and optimized for performance
- π i18n Support - Internationalization-friendly routing patterns
npm install 07-navegation-routeryarn add 07-navegation-routerpnpm add 07-navegation-routerπ Full documentation and interactive examples are available on our GitHub Pages site:
The documentation site includes:
- π Complete API Documentation - Detailed guides for all components
- π» Interactive Examples - Try the router live in your browser
- π― Basic Routing Example - Get started quickly
- π Dynamic Routes Example - Learn parameter handling
- π‘οΈ Error Handling Example - Robust error boundary patterns
- β‘ Performance Benchmarks - See how fast the router is
- π TypeScript Support - Full type definitions and examples
import { Router, Route, Link } from "07-navegation-router";
function App() {
return (
<Router>
<nav>
<Link to="/">Home</Link>
<Link to="/about">About</Link>
<Link to="/users/123">User Profile</Link>
</nav>
<Route path="/" Component={Home} />
<Route path="/about" Component={About} />
<Route path="/users/:id" Component={UserProfile} />
<Route path="/404" Component={NotFound} />
</Router>
);
}
function Home() {
return (
<div>
<h1>Home Page</h1>
<Link to="/about">Go to About</Link>
</div>
);
}
function About() {
return (
<div>
<h1>About Page</h1>
<Link to="/">Back to Home</Link>
</div>
);
}
function UserProfile({ routeParams }) {
// Access route parameters
return <h1>User Profile #{routeParams.id}</h1>;
}
function NotFound() {
return <h1>404 - Page Not Found</h1>;
}The main container component that enables routing in your application.
<Router defaultComponent={NotFound}>{/* Your routes go here */}</Router>Props:
defaultComponent(React.Component, optional): Component to render when no route matches (404 handler)routes(Array, optional): Array of route objects for programmatic routingchildren(React.Node): Route components
Defines a route with a specific path and component to render.
Props:
path(string): The URL path pattern (supports parameters like/users/:id)Component(React.Component): The component to render when the path matches
<Route path="/products/:id" Component={ProductDetail} />Route Parameters:
The matched component receives routeParams as a prop:
function ProductDetail({ routeParams }) {
const { id } = routeParams;
return <h1>Product {id}</h1>;
}Declarative navigation component for client-side routing without page reloads.
Props:
to(string): The destination pathtarget(string, optional): Link target attribute (e.g.,_blank)- Any other props are passed to the underlying
<a>element
<Link to="/about">About Us</Link>
<Link to="/docs" target="_blank">Documentation</Link>
<Link to="/contact" className="nav-link">Contact</Link>Programmatic navigation function for imperatively changing routes.
import { navigate } from "07-navegation-router";
function handleLogin() {
// Perform login logic
navigate("/dashboard");
}Error boundary component for catching and handling routing errors gracefully.
Props:
fallback(React.Component | React.Element, optional): Custom error fallback UIchildren(React.Node): Components to wrap
import { Router, Route, RouterErrorBoundary } from "07-navegation-router";
function App() {
return (
<RouterErrorBoundary fallback={<ErrorPage />}>
<Router>
<Route path="/" Component={Home} />
<Route path="/error" Component={BuggyComponent} />
</Router>
</RouterErrorBoundary>
);
}
function ErrorPage({ error, resetError }) {
return (
<div>
<h1>Something went wrong</h1>
<pre>{error.message}</pre>
<button onClick={resetError}>Try again</button>
</div>
);
}Access route parameters in your components:
import { Router, Route, Link } from "07-navegation-router";
function App() {
return (
<Router>
<Route path="/blog/:slug" Component={BlogPost} />
<Route path="/users/:userId/posts/:postId" Component={UserPost} />
</Router>
);
}
function BlogPost({ routeParams }) {
const { slug } = routeParams;
return <article>Post: {slug}</article>;
}
function UserPost({ routeParams }) {
const { userId, postId } = routeParams;
return (
<div>
User {userId}, Post {postId}
</div>
);
}Improve performance by code-splitting your routes:
import { lazy, Suspense } from "react";
const Dashboard = lazy(() => import("./pages/Dashboard"));
function App() {
return (
<Router>
<Suspense fallback={<div>Loading...</div>}>
<Route path="/dashboard" Component={Dashboard} />
</Suspense>
</Router>
);
}Define a default route for unmatched paths:
<Router>
<Route path="/" Component={Home} />
<Route path="/about" Component={About} />
<Route path="/404" Component={NotFound} />
</Router>Use the navigate function for imperative routing:
import { navigate } from "07-navegation-router";
function LoginForm() {
const handleSubmit = async (e) => {
e.preventDefault();
const success = await login();
if (success) {
navigate("/dashboard");
window.scrollTo(0, 0); // Optional: scroll to top
}
};
return <form onSubmit={handleSubmit}>...</form>;
}
// Navigate with state
function goToProfile() {
navigate("/profile");
// The navigation event is automatically dispatched
}Protect your routes with error boundaries:
import { RouterErrorBoundary, Router, Route } from "07-navegation-router";
function App() {
return (
<RouterErrorBoundary
fallback={({ error, resetError }) => (
<div>
<h2>Oops! Something went wrong</h2>
<details>
<summary>Error details</summary>
<pre>{error.message}</pre>
</details>
<button onClick={resetError}>Reset</button>
<button onClick={() => navigate("/")}>Go Home</button>
</div>
)}
>
<Router>
<Route path="/" Component={Home} />
<Route path="/risky" Component={RiskyComponent} />
</Router>
</RouterErrorBoundary>
);
}Full TypeScript definitions are included:
import {
Router,
Route,
Link,
navigate,
RouterErrorBoundary,
} from "07-navegation-router";
import type { RouteParams, RouteComponentProps } from "07-navegation-router";
interface UserProfileProps extends RouteComponentProps {
routeParams: {
id: string;
};
}
const UserProfile: React.FC<UserProfileProps> = ({ routeParams }) => {
return <h1>User {routeParams.id}</h1>;
};
// Type-safe navigation
navigate("/users/123");navegation-router/
βββ src/
β βββ components/
β β βββ Router.jsx # Main router component
β β βββ Route.jsx # Route definition component
β β βββ Link.jsx # Navigation link component
β β βββ ErrorBoundary.jsx # Error boundary component
β β βββ Router.test.jsx # Router tests (13 tests)
β β βββ Link.test.jsx # Link tests (10 tests)
β β βββ Route.test.jsx # Route tests (5 tests)
β βββ utils/
β β βββ consts.js # Constants
β β βββ getCurrentPath.js # Path utility
β β βββ navigation.js # Navigation utilities
β βββ index.jsx # Main entry point
β βββ navigation.js # Navigation exports
βββ lib/ # Compiled output (published to npm)
βββ docs/ # Documentation site
βββ examples/ # Interactive examples
βββ benchmarks/ # Performance benchmarks
βββ .github/workflows/ # CI/CD pipelines
β βββ ci.yml # Test & lint automation
β βββ pages.yml # GitHub Pages deployment
βββ package.json
βββ LICENSE # MIT License
βββ CHANGELOG.md # Version history
βββ CONTRIBUTING.md # Contribution guidelines
βββ README.md
This library is thoroughly tested using:
- Vitest - Fast unit test framework
- Happy DOM - Lightweight DOM implementation
- Testing Library - React component testing utilities
Test Coverage:
- β 28 tests passing
- β Router component (13 tests)
- β Link component (10 tests)
- β Route component (5 tests)
npm run test # Run tests once
npm run test:watch # Watch mode
npm run test:ui # UI mode
npm run bench # Run performance benchmarksBenchmarks show excellent performance:
- Route Matching: ~0.05ms per route
- Bundle Size: 5.9 kB gzipped
- Tree-shakeable: Import only what you need
Run benchmarks yourself:
npm run bench# Install dependencies
npm install
# Run development server
npm run dev
# Build the library
npm run prepare
# Run tests
npm run test- β Chrome (latest)
- β Firefox (latest)
- β Safari (latest)
- β Edge (latest)
Contributions are welcome! Please read our Contributing Guidelines before submitting a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Write tests for your changes
- Ensure all tests pass (
npm test) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
See CONTRIBUTING.md for detailed guidelines.
This project is licensed under the MIT License - see the LICENSE file for details.
Carlos CaΓ±o GΓ³mez
- GitHub: @CarlosCG2000
- npm: 07-navegation-router
- π Official Documentation Site - Complete guides and API reference
- π» Interactive Examples - Try the router in your browser
- οΏ½ Changelog - Version history and updates
- π¦ npm Package - Install from npm registry
- οΏ½ GitHub Repository - Source code
- οΏ½π Report Issues - Bug reports and feature requests
- π‘ Contributing Guide - How to contribute
- β Star on GitHub - Show your support!
- β CI Pipeline - Automated testing
- π Pages Deployment - Documentation site status
- Special thanks to Midudev for his invaluable educational content and didactic approach to teaching React and modern web development. His tutorials and courses were a significant inspiration for this project.
- Inspired by React Router and modern routing libraries
- Built with modern React best practices and hooks
- Powered by
path-to-regexpfor advanced pattern matching - Tested with Vitest and React Testing Library
- Compiled with SWC for blazing-fast builds
- Midudev's YouTube Channel - Excellent React tutorials in Spanish
- Midudev's Courses - In-depth web development courses
See CHANGELOG.md for a detailed version history.
Latest Release: v0.3.1
- β TypeScript definitions
- β Error boundary support
- β 28 comprehensive tests
- β Performance benchmarks
- β Interactive examples
- β CI/CD automation
- React Router - The most popular React routing library
- Wouter - Minimalist routing for React
- Reach Router - Accessible routing library
react, router, routing, spa, single-page-application, navigation, react-router, client-side-routing, declarative-routing, dynamic-routes, lazy-loading, history-api, browser-history, lightweight-router, minimal-router
Visit our Documentation Site for:
- π Complete guides and tutorials
- π» Live, interactive examples you can try in your browser
- π― Step-by-step implementation guides
- π‘οΈ Best practices and patterns
- β‘ Performance optimization tips
Made with β€οΈ by Carlos CaΓ±o GΓ³mez
If you find this project useful, please consider:
- β Starring the GitHub Repository
- π’ Sharing it with other developers
- π Reporting issues to help improve it
- π€ Contributing to the project
Happy routing! π