Convert Figma logo to code with AI

router5 logorouter5

Flexible and powerful universal routing solution

1,717
105
1,717
104

Top Related Projects

Declarative routing for React

6,895

6,666

🥢 A minimalist-friendly ~2.1KB routing for React and Preact

A simple middleware-style router for isomorphic JavaScript web apps

2,744

A simple vanilla JavaScript router.

Quick Overview

Router5 is a flexible and powerful routing solution for JavaScript applications. It is framework-agnostic and can be used with various UI libraries or vanilla JavaScript. Router5 focuses on simplicity, modularity, and performance, making it suitable for both small and large-scale applications.

Pros

  • Framework-agnostic, can be used with any UI library or vanilla JavaScript
  • Lightweight and performant
  • Supports nested routes and route parameters
  • Extensive plugin ecosystem for additional functionality

Cons

  • Steeper learning curve compared to some framework-specific routers
  • Less opinionated, which may require more setup and configuration
  • Smaller community compared to more popular routing solutions
  • Documentation could be more comprehensive for advanced use cases

Code Examples

  1. Basic route configuration:
import createRouter from 'router5';
import browserPlugin from 'router5-plugin-browser';

const routes = [
  { name: 'home', path: '/' },
  { name: 'about', path: '/about' },
  { name: 'users', path: '/users' },
  { name: 'users.view', path: '/:id' }
];

const router = createRouter(routes);
router.usePlugin(browserPlugin());
  1. Starting the router and handling navigation:
router.start((err, state) => {
  if (err) {
    console.error('Router failed to start', err);
  } else {
    console.log('Router started', state);
  }
});

router.navigate('users.view', { id: '123' }, (err, state) => {
  if (err) {
    console.error('Navigation failed', err);
  } else {
    console.log('Navigated to', state);
  }
});
  1. Using middleware:
const authMiddleware = (router, dependencies) => (toState, fromState, done) => {
  if (toState.name === 'admin' && !dependencies.isAuthenticated()) {
    done({ redirect: { name: 'login' } });
  } else {
    done();
  }
};

router.useMiddleware(authMiddleware);

Getting Started

To get started with Router5, follow these steps:

  1. Install Router5 and the browser plugin:

    npm install router5 router5-plugin-browser
    
  2. Create a router instance with routes and plugins:

    import createRouter from 'router5';
    import browserPlugin from 'router5-plugin-browser';
    
    const routes = [
      { name: 'home', path: '/' },
      { name: 'about', path: '/about' }
    ];
    
    const router = createRouter(routes);
    router.usePlugin(browserPlugin());
    
  3. Start the router:

    router.start();
    
  4. Use the router to navigate between routes:

    router.navigate('about');
    

Competitor Comparisons

Declarative routing for React

Pros of React Router

  • Deeply integrated with React ecosystem, offering seamless component-based routing
  • Extensive documentation and large community support
  • Supports nested routing and dynamic route matching

Cons of React Router

  • Larger bundle size compared to Router5
  • More opinionated approach, which may limit flexibility in some scenarios
  • Steeper learning curve for developers new to React

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>

Router5:

import createRouter from 'router5';
import browserPlugin from 'router5-plugin-browser';

const router = createRouter([
  { name: 'home', path: '/' },
  { name: 'about', path: '/about' }
]);
router.usePlugin(browserPlugin());
router.start();

React Router focuses on declarative, component-based routing within React applications, while Router5 provides a more generic routing solution that can be used with various frameworks. React Router's tight integration with React makes it a popular choice for React developers, but Router5's lightweight and framework-agnostic approach offers more flexibility for projects using different technologies or requiring custom routing logic.

6,895

Pros of Reach Router

  • Designed specifically for React applications, offering seamless integration
  • Provides accessible navigation out of the box
  • Supports relative routing, making nested routes easier to manage

Cons of Reach Router

  • Less flexible for non-React applications
  • Smaller community and ecosystem compared to Router5
  • Limited support for complex routing scenarios

Code Comparison

Reach Router:

import { Router, Link } from "@reach/router"

const App = () => (
  <Router>
    <Home path="/" />
    <Dashboard path="dashboard" />
  </Router>
)

Router5:

import createRouter from 'router5'

const routes = [
  { name: 'home', path: '/' },
  { name: 'dashboard', path: '/dashboard' }
]
const router = createRouter(routes)

Key Differences

  • Reach Router is React-specific, while Router5 is framework-agnostic
  • Router5 offers more granular control over routing behavior
  • Reach Router provides built-in accessibility features
  • Router5 has a larger ecosystem and community support
  • Reach Router uses a component-based approach, while Router5 uses a configuration object

Both routers have their strengths, with Reach Router excelling in React applications and accessibility, while Router5 offers more flexibility and control across different frameworks.

6,666

🥢 A minimalist-friendly ~2.1KB routing for React and Preact

Pros of wouter

  • Lightweight and minimalistic, with a smaller bundle size
  • Simple API, easy to learn and use
  • Hooks-based approach, aligning well with modern React practices

Cons of wouter

  • Less feature-rich compared to router5
  • May require additional libraries for more complex routing scenarios
  • Limited support for nested routing

Code Comparison

wouter:

import { Route, Switch } from "wouter";

<Switch>
  <Route path="/users/:id" component={UserProfile} />
  <Route path="/about" component={About} />
</Switch>

router5:

import { RouterProvider, Route } from "react-router5";

<RouterProvider router={router}>
  <Route name="users.view" component={UserProfile} />
  <Route name="about" component={About} />
</RouterProvider>

Summary

wouter is a lightweight, hooks-based routing solution for React applications, offering simplicity and ease of use. It's ideal for smaller projects or those preferring a minimalistic approach. router5, on the other hand, provides a more comprehensive routing solution with advanced features, making it suitable for complex applications with intricate routing requirements. The choice between the two depends on the specific needs of your project, with wouter excelling in simplicity and router5 in feature richness.

A simple middleware-style router for isomorphic JavaScript web apps

Pros of Universal Router

  • Lightweight and flexible, with a smaller footprint than Router5
  • Supports both sync and async route resolution
  • Easy integration with React and other view libraries

Cons of Universal Router

  • Less feature-rich compared to Router5
  • Requires more manual setup for complex routing scenarios
  • Limited built-in support for nested routes

Code Comparison

Universal Router:

const router = new UniversalRouter([
  { path: '/home', action: () => '<h1>Home</h1>' },
  { path: '/posts', action: () => '<h1>Posts</h1>' },
]);

router.resolve('/home').then(html => {
  document.body.innerHTML = html;
});

Router5:

const router = createRouter([
  { name: 'home', path: '/home' },
  { name: 'posts', path: '/posts' },
]);

router.start();
router.navigate('home');

Universal Router offers a more straightforward approach to defining routes and actions, while Router5 provides a more structured setup with named routes. Universal Router's promise-based resolution allows for easy async handling, whereas Router5 uses a more traditional event-based system for navigation.

Both routers have their strengths, with Universal Router being more lightweight and flexible, and Router5 offering a more comprehensive feature set for complex applications.

2,744

A simple vanilla JavaScript router.

Pros of Navigo

  • Lightweight and simple to use, with a smaller learning curve
  • Supports hash-based routing out of the box
  • Easier setup for basic routing needs

Cons of Navigo

  • Less powerful for complex routing scenarios
  • Fewer built-in features compared to Router5
  • Limited middleware support

Code Comparison

Navigo:

const router = new Navigo('/');
router.on('/user/:id', (params) => {
  console.log(`User ID: ${params.id}`);
}).resolve();

Router5:

const router = createRouter([
  { name: 'user', path: '/user/:id' }
]);
router.start();
router.subscribe(({ route }) => {
  console.log(`User ID: ${route.params.id}`);
});

Key Differences

  • Navigo uses a more straightforward API for defining routes
  • Router5 employs a more structured approach with named routes
  • Navigo's route handling is more direct, while Router5 uses a subscription model
  • Router5 offers more advanced features like middleware and plugins
  • Navigo is better suited for smaller projects, while Router5 excels in larger, more complex applications

Both routers have their strengths, and the choice between them depends on the specific needs of your project. Navigo is ideal for simpler routing requirements, while Router5 provides more flexibility and power for complex routing scenarios.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Router5

npm version License: MIT Build Status Join the chat at https://gitter.im/router5/router5 styled with prettier

Official website: router5.js.org

router5 is a framework and view library agnostic router.

  • view / state separation: router5 processes routing instructions and outputs state updates.
  • universal: works client-side and server-side
  • simple: define your routes, start to listen to route changes
  • flexible: you have control over transitions and what happens on transitions
import createRouter from 'router5'
import browserPlugin from 'router5-plugin-browser'

const routes = [
  { name: 'home', path: '/' },
  { name: 'profile', path: '/profile' }
]

const router = createRouter(routes)

router.usePlugin(browserPlugin())

router.start()

With React (hooks)

import React from 'react'
import ReactDOM from 'react-dom'
import { RouterProvider, useRoute } from 'react-router5'

function App() {
  const { route } = useRoute()

  if (!route) {
    return null
  }

  if (route.name === 'home') {
    return <h1>Home</h1>
  }

  if (route.name === 'profile') {
    return <h1>Profile</h1>
  }
}

ReactDOM.render(
  <RouterProvider router={router}>
    <App />
  </RouterProvider>,
  document.getElementById('root')
)

With observables

Your router instance is compatible with most observable libraries.

import { from } from 'rxjs/observable/from'

from(router).map(({ route }) => {
  /* happy routing */
})

Examples

Docs

NPM DownloadsLast 30 Days