Top Related Projects
Declarative routing for React
🥢 A minimalist-friendly ~2.1KB routing for React and Preact
A simple middleware-style router for isomorphic JavaScript web apps
Quick Overview
Navigo is a lightweight JavaScript router for single-page applications. It provides a simple and flexible way to handle client-side routing, allowing developers to create dynamic web applications with clean URLs and smooth navigation without page reloads.
Pros
- Lightweight and fast, with minimal overhead
- Easy to use and integrate into existing projects
- Supports both hash-based and history API-based routing
- Allows for nested routes and parameterized URLs
Cons
- Limited built-in features compared to more comprehensive routing libraries
- May require additional setup for server-side rendering
- Documentation could be more extensive and provide more advanced examples
- Smaller community compared to some popular alternatives
Code Examples
- Basic route setup:
import Navigo from 'navigo';
const router = new Navigo('/');
router
.on('/', () => { console.log('Home page'); })
.on('/about', () => { console.log('About page'); })
.on('/contact', () => { console.log('Contact page'); })
.resolve();
- Parameterized routes:
router.on('/user/:id', (params) => {
console.log(`User profile for ID: ${params.id}`);
});
- Nested routes:
router
.on('/products', () => { console.log('Products list'); })
.on('/products/:category', (params) => {
console.log(`Products in category: ${params.category}`);
})
.on('/products/:category/:id', (params) => {
console.log(`Product ${params.id} in category ${params.category}`);
});
Getting Started
- Install Navigo:
npm install navigo
- Import and initialize the router:
import Navigo from 'navigo';
const router = new Navigo('/');
// Define your routes
router
.on('/', () => { /* render home page */ })
.on('/about', () => { /* render about page */ })
.on('/contact', () => { /* render contact page */ });
// Start the router
router.resolve();
- Update your HTML to use Navigo links:
<a href="/" data-navigo>Home</a>
<a href="/about" data-navigo>About</a>
<a href="/contact" data-navigo>Contact</a>
Competitor Comparisons
Pros of Reach Router
- Built specifically for React, offering seamless integration and optimized performance
- Provides accessible navigation out of the box, enhancing user experience for all
- Supports nested routes and relative navigation, simplifying complex routing scenarios
Cons of Reach Router
- Limited to React applications, unlike Navigo which is framework-agnostic
- Larger bundle size compared to Navigo's lightweight footprint
- Steeper learning curve for developers new to React routing concepts
Code Comparison
Reach Router:
import { Router, Link } from "@reach/router"
const App = () => (
<Router>
<Home path="/" />
<Dashboard path="dashboard" />
</Router>
)
Navigo:
import Navigo from "navigo"
const router = new Navigo("/")
router
.on("/", () => { /* render home */ })
.on("/dashboard", () => { /* render dashboard */ })
Key Differences
- Reach Router uses JSX syntax and component-based routing, while Navigo uses a more traditional JavaScript approach
- Navigo offers more flexibility for use in various JavaScript environments, whereas Reach Router is React-specific
- Reach Router provides built-in accessibility features, which are not inherently present in Navigo
Declarative routing for React
Pros of React Router
- More comprehensive routing solution with advanced features like nested routes and route-based code splitting
- Larger community and ecosystem, with extensive documentation and third-party integrations
- Seamless integration with React and its component-based architecture
Cons of React Router
- Steeper learning curve, especially for beginners or those new to React
- Larger bundle size, which may impact initial load times for smaller applications
- More opinionated approach, which can be limiting for certain use cases
Code Comparison
React Router:
import { BrowserRouter, Route, Switch } from 'react-router-dom';
<BrowserRouter>
<Switch>
<Route path="/about" component={About} />
<Route path="/" exact component={Home} />
</Switch>
</BrowserRouter>
Navigo:
import Navigo from 'navigo';
const router = new Navigo('/');
router
.on('/about', () => { /* render About page */ })
.on('/', () => { /* render Home page */ })
.resolve();
React Router offers a more declarative approach, integrating directly with React components. Navigo, on the other hand, provides a simpler, more imperative API that can be used with any JavaScript framework or vanilla JS. React Router's syntax is more React-centric, while Navigo's is more flexible and framework-agnostic.
🥢 A minimalist-friendly ~2.1KB routing for React and Preact
Pros of wouter
- Lightweight and minimalistic, with a smaller bundle size
- Supports React hooks out of the box
- Simple API that's easy to learn and use
Cons of wouter
- Less feature-rich compared to Navigo
- Limited support for more complex routing scenarios
- Fewer configuration options for advanced use cases
Code Comparison
Navigo:
const router = new Navigo("/");
router.on("/user/:id", ({ data }) => {
console.log(`User ID: ${data.id}`);
}).resolve();
wouter:
import { Route, Switch } from "wouter";
function App() {
return (
<Switch>
<Route path="/user/:id">
{(params) => <div>User ID: {params.id}</div>}
</Route>
</Switch>
);
}
Both Navigo and wouter are routing libraries for JavaScript applications, but they have different approaches and target audiences. Navigo is a more feature-rich router with support for various routing scenarios, while wouter focuses on simplicity and lightweight implementation, particularly for React applications. The code comparison shows that Navigo uses a more imperative approach, while wouter leverages React components and hooks for routing. Choose the library that best fits your project's requirements and complexity.
A simple middleware-style router for isomorphic JavaScript web apps
Pros of Universal Router
- More flexible and powerful routing system, supporting nested routes and complex patterns
- Better integration with modern JavaScript frameworks like React and Vue.js
- Supports asynchronous route resolution, allowing for dynamic loading of components
Cons of Universal Router
- Steeper learning curve due to its more complex API
- Requires more setup and configuration compared to Navigo's simpler approach
- Larger bundle size, which may impact performance for smaller applications
Code Comparison
Navigo:
const router = new Navigo('/');
router.on('/user/:id', (params) => {
console.log(`User ID: ${params.id}`);
});
router.resolve();
Universal Router:
const router = new UniversalRouter([
{ path: '/user/:id', action: (context) => {
console.log(`User ID: ${context.params.id}`);
}}
]);
router.resolve('/user/123');
Both routers provide similar basic functionality, but Universal Router offers more advanced features and flexibility at the cost of increased complexity. Navigo is simpler to set up and use, making it a good choice for smaller projects or developers new to routing. Universal Router is better suited for larger, more complex applications that require advanced routing capabilities and integration with modern JavaScript frameworks.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Navigo
A simple dependency-free minimalistic JavaScript router
- v.8+ documentation
- v.7 documentation
- Examples
- Changelog
- Using React? Checkout navigo-react package.
- ð® Online playground here codesandbox.io/s/navigo-example-jrui8;
Selling points
- Dependency free
- ~10KB minified, ~4KB gzipped
- Based on History API so it does update the URL of the page
- Supports hash based routing too
- Simple mapping of route to a function call
- Parameterized routes
- Navigating between routes
- Hooks (before, after, leave, already)
- Not-found and default handler
- Easy integration with HTML links via
data-navigo
HTML attribute
Installation
Drop the following into your page:
<script src="//unpkg.com/navigo"></script>
or via npm/yarn:
> npm install navigo --save
> yarn add navigo -S
Quick start
import Navigo from 'navigo'; // When using ES modules.
const router = new Navigo('/');
The constructor of the library accepts a single argument - the root path of your app. If you host your project at https://site.com/my/awesome/app
, your root path is /my/awesome/app
. Then you have to define your routes.
router.on('/my/awesome/app', function () {
// do something
});
At the end you have to trigger the resolving logic:
router.resolve();
After that when you need a page change call the navigate
method. This one changes the URL and (by default) triggers resolve
.
router.navigate('/about');
Add data-navigo
attribute to your page links and they'll be transformed into navigate
callers.
<a href="/about/contacts" data-navigo>Contacts</a>
Checkout the online playground to see it in action.
Development
> yarn dev
Building
> yarn build
Tests
> yarn test
> yarn test-watch
MISC
Top Related Projects
Declarative routing for React
🥢 A minimalist-friendly ~2.1KB routing for React and Preact
A simple middleware-style router for isomorphic JavaScript web apps
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot