React Easy Router

2.2.0 · active · verified Sun Apr 19

react-easy-router is a declarative routing library for React applications that significantly simplifies the configuration of routes by building upon `react-router-dom`. Currently at version 2.2.0, it offers an object-based approach to defining routes, supporting dynamic segments, navigation, redirection, and protected routes with optional role-based access control. Recent releases indicate an active development cycle, with version 2.1.0 converting the entire architecture to TypeScript and subsequent releases adding features like multiple role authentication and type improvements. It differentiates itself by abstracting away much of `react-router-dom`'s direct component usage, providing a more concise API for common routing patterns and built-in authentication hooks. This package requires `react-router-dom` version 6.4.0 or later to be installed as a peer dependency, and the application must be wrapped in `BrowserRouter` externally.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of `react-easy-router` with a defined array of routes, including nested, dynamic, and protected routes with role-based authentication. It also correctly wraps the application in `BrowserRouter` as required.

import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom';
import Router from 'react-easy-router';

// Placeholder components
const Login = () => <div>Login Page</div>;
const Admins = () => <div>Admins Dashboard</div>;
const Admin = () => <div>Admin Detail</div>;
const Users = () => <div>Users List</div>;
const User = () => <div>User Detail</div>;
const NotFound = () => <div>404 - Page Not Found</div>;

const routes = [
  { path: '/', navigate: '/login' },
  { path: '/login', element: <Login /> },
  {
    path: '/admins',
    element: <Admins />,
    children: [{ path: '/admin', element: <Admin /> }],
  },
  {
    path: '/users',
    element: <Users />,
    children: [{ path: '/user', element: <User /> }],
  },
  {
    path: '/admin',
    element: <Admin />,
    protected: true,
    roles: ['admin', 'manager'], // Optional: Role specific screen
    failureRedirect: '/login', // Optional: Default is '/' if not specified
  },
  { path: '*', element: <NotFound /> },
];

function App() {
  // Function can resolve/reject or return true/false
  const checkAuth = () => {
    const token = typeof window !== 'undefined' ? localStorage.getItem('token') : null;

    // Simulate authentication based on a token and return a role
    if (token === 'admin-token') {
      return { success: true, role: 'admin' };
    } else if (token === 'manager-token') {
      return { success: true, role: 'manager' };
    } else {
      return false; // Not authenticated or no valid token
    }
  };

  return <Router routes={routes} isAuthenticated={checkAuth} />;
}

// Render the app
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
root.render(
  <React.StrictMode>
    <BrowserRouter>
      <App />
    </BrowserRouter>
  </React.StrictMode>
);

view raw JSON →