npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-router-dom-jsonroutes

v0.1.7

Published

Create a centralized routing config with React-Router-DOM-JSONRoutes

Downloads

47

Readme

Create a centralized routing config with React-Router-DOM-JSONRoutes

react-router-dom-jsonroutes is the simplified way of handling all your multi-nested routes in one single place as JSON data, RRDJR comes with only two props routesList and authMiddleware

Feature highlights

This library comes with 4 important features

  • [x] JSON routing (centralized routing)
  • [x] Authentication
  • [x] Layout Routes
  • [x] Index Route

Install

[!NOTE]
This package works best with [email protected]

npm install react-router-dom-jsonroutes --save

Quick Example

Step 1: Import the library and your favorite route components

import { BrowserRouter, Navigate } from "react-router-dom";
import JsonRoutes from "react-router-dom-jsonroutes";

/* import your components here */
const Login from "../modules/Login";
const Dashboard from "../modules/Dashboard";

Step 2: Create routesList JSON object with all your application routes, see Nested Examples here

/* add all your application routes as json data */
const routesList = [{
    path: 'login',
    component: Login,
    secure: false
}, {
    path: 'dashboard',
    component: Dashboard,
    secure: true
}]

Step 3: Also create an authMiddleware this will help to make decisions when you have private/secure routes, and also helps when you have role-based authentication

/* create a authMiddle if required (optional) */
const authMiddleware = ({ component: Component, secure = false }) => {
    {/* improve your auth logic here */}
    let isLogin = true;

    if (secure) {
        return isLogin ? <Component /> : <Navigate to="/login" />
    }

    return !isLogin ? <Component /> : <Navigate to="/dashboard" />
}

Step 4: Finally, return your component as shown bellow

return(
    <BrowserRouter>
        {/* render RRDJR as child inside BrowserRouter */}
        <JsonRoutes routesList={routesList} authMiddleware={authMiddleware} />
    </BrowserRouter>
)

JSON Routing

We found value in having a centralized route configuration, A route is just data. React is great at mapping data into components, and is a component.

Route config is just an array of logical “routes” with path, component, secure and index props same way as you would add to any component.

Basic Example

const routesList = [{
    path: 'login',
    component: Login,
    secure: false
},{
    path: 'dashboard',
    component: Dashboard,
    secure: true
}]

Nested Example

const routesList = [{
    path: 'dashboard',
    component: Dashboard,
    secure: true
},{
    path: 'products',
    secure: true,
    component: Products,
    children: [
        {
            path: ':id',
            secure: true,
            component: ProductsSingle
        },
        {
            path: 'edit',
            secure: false,
            component: ProductsSingle
        },
        {
            path: 'cart',
            component: ProductsSingle
        }
    ]
}]

Authentication

Why not have a centralization routing config? With authentication capabilities as a win-win for developers, RRDJR comes with authMiddleware prop which helps you to navigate users if not logged In.

Basic Example

const authMiddleware = ({ component: Component, secure = false, user }) => {
    let isLogin = true;

    if (secure) {
        return isLogin ? <Component /> : <Navigate to="/login" />
    }
    return !isLogin ? <Component /> : <Navigate to="/dashboard" />
}

Redux Example

const authMiddleware = connect((state) => ({
    user: state.auth.user
}), {})(({ component: Component, secure = false, user }) => {
    let hasUserDetails = user._id ? true : false

    if (secure) {
        return hasUserDetails ? <Component /> : <Navigate to="/login" />
    }
    return !hasUserDetails ? <Component /> : <Navigate to="/dashboard" />
})

Layout Routes

Layout routes are parent routes used exclusively for grouping child routes inside a specific layout.

Basic Example

const routesList = [{
    path: 'settings',
    secure: true, // if true every nested route will be `secure`
    wrapper: Settings,
    children: [
        {
            path: 'profile',
            component: Profile
        }
    ]
}, {
    path: 'docs',
    secure: false, // if false child routes can have `secure` true || false
    wrapper: Teams,
    children: [{
        path: 'api',
        secure: true,
        component: APIDocs
    }, {
        path: 'get-started',
        component: GetStarted
    }]
}]

then, To render the child, you need to render an <Outlet /> component

import { Outlet } from "react-router-dom";

const Settings = () => {
    return(
        <div>
            <h1>Settings</h1>
            <Outlet />
        </div>
    )
}

Index Route

A child route with no path that renders in the parent's <Outlet /> at the parent's URL.

Basic Example

const routesList = [{
    path: 'docs',
    wrapper: Teams,
    children: [{
        path: 'api',
        component: APIDocs
    }, {
        index: true,
        component: GetStarted // this will render on `/docs` inside `<Outlet />`
    }]
}]