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-mapping

v3.0.4

Published

This library has as auxiliary tools the developers to have a better way to map, group and manage the routes of their applications.

Downloads

161

Readme

React Router Mapping

This library has as auxiliary tools the developers to have a better way to map, group and manage the routes of their applications.

Before starting

This library works in conjunction with React Router, using certain features that would not need to be rewritten. Therefore, carefully read each section of this document.

Installation

npm install react-router-mapping

How to use

import React from 'react';
import { BrowserRouter, Routes } from 'react-router-dom';
import { MappingProvider, useMap } from 'react-router-mapping';

export default () => {

  const [ MainRoutes, mapRoutes ] = useMap([
    {
      name : 'home',
      label : 'Home',
      path : '/',
      element : <Home />,
    },
    {
      path : '/route-one',
      element : <Template />,
      routes : [
        {
          name : 'route-one',
          label : 'Route One',
          element : <RouteOne />,
          index : true,
        },
        {
          name : 'sub-route-one',
          label : 'Sub Route One',
          path : 'sub-route-one',
          element : <SubRouteOne />,
        },
      ],
      {
        path : '/:id',
        routes : [
          {
            name : 'route-two',
            label : 'Route Two',
            element : <RouteTwo />,
            index : true,
          },
          {
            name : 'sub-route-two',
            label : 'Sub Route Two',
            path : 'sub-route-two',
            element : <SubRouteTwo />,
          },
        ],
      },
    }
  ]);

  return (
    <BrowserRouter>
      <MappingProvider routes={mapRoutes}>
        <Routes>
          {MainRoutes}
        </Routes>
      </MappingProvider>
    </BrowserRouter>
  );
};

With multiple maps

import React from 'react';
import { BrowserRouter, Routes } from 'react-router-dom';
import { MappingProvider, useMap } from 'react-router-mapping';

export default () => {

  const [ MainRoutes, mapRoutes ] = useMap([
    ...
  ]);

  const [ OtherRoutes, otherMapRoutes ] = useMap([
    ...
  ]);

  return (
    <BrowserRouter>
      <MappingProvider routes={[ mapRoutes, otlherMapRoutes ]}>
        <Routes>
          {MainRoutes}
          {OtherRoutes}
        </Routes>
      </MappingProvider>
    </BrowserRouter>
  );
};

For security, routes containing * are not added to the Map constructor returned by the useMap hook. This can confuse the breadcrumb hook.

Components

MappingProvider (Required)

Responsible for the functional context of the library, without it, any and all functionality will be invalid.

Properties

| Property | Type | Description | Default | | ------ | ------ | ------ | ------ | | routes | Object/Array | Receives an object or an array of Map objects, which is created by the useMap hook, which contains the handled routes to be served by the context. | Required |

Hooks

useMap() (Required)

Is the main hook for the proper functioning of the library. It takes only a single argument in its use, an array of objects, where any and all properties are the same as the Route component of the React Router. However, there are 2 new properties to be included that are necessary for the use of the other hooks that will be described later. Its return is an object with each of the routes informed in its argument, each property returned is equivalent to each of the routes informed and all already treated with the Route component.

Properties

| Property | Type | Description | Default | | ------ | ------ | ------ | ------ | | name | String | Key value for route identification | Required | | label | String | Friendly title for breadcrumb use | Empty |

useRoute()

Is the hook responsible for allowing you to access the routes object generated by the library. Below I describe their methods:

all()

This method returns a list with all the routes that the application has, for example:

import { useEffect } from 'react';
import { useRoute } from 'react-router-mapping';

export default () => {

  const { all } = useRoute();
	
  useEffect(() => {
    console.log(all());
  }, []);
  ...
}

Return

{
  "home": {
    "name": "home",
    "path": "/",
    "label": "Home"
  },
  "route-one": {
    "name": "route-one",
    "path": "/route-one",
    "label": "Route One"
  },
  "sub-route-one": {
    "name": "sub-route-one",
    "path": "/route-one/sub-route-one",
    "label": "Sub Route One"
  },
  "route-two": {
    "name": "route-two",
    "path": "/:id/route-two",
    "label": "Route Two"
  },
  "sub-route-two": {
    "name": "sub-route-two",
    "path": "/:id/route-two/sub-route-two",
    "label": "Sub Route Two"
  },
}

route(name:string, params:object)

This method has 2 (two) distinct features, the first would be passing a value to the first name argument, see below for a practical example:

import { useEffect } from 'react';
import { useRoute } from 'react-router-mapping';

export default () => {

  const { route } = useRoute();

  useEffect(() => {
    console.log(route('sub-route-one'));
  }, []);
  ...
}

Return

/route-one/sub-route-one

The second and last feature would be a complement to dynamic parameters in the route, to perform a substitution of values ​​in the routes, just use the second argument params as an object and put the property with the name of the desired parameter and its respective value, see the example:

import { useEffect } from 'react';
import { useRoute } from 'react-router-mapping';

export default () => {

  const { route } = useRoute();

  useEffect(() => {
    console.log('Without params: ', route('sub-route-two'));
    console.log('With params: ', route('sub-route-two', { id : 'DEF-456' }));
  }, []);
  ...
}

Return

Without params: /ABC-123/route-two/sub-route-two
...
With params: /DEF-456/route-two/sub-route-two

useBreadcrumb()

As the name says, this is a hook for breadcrump, without much secret, it returns an array of objects containing the full path of the user's location in the application, a practical example, let's say the user is on the 'Nested Route':

import { useEffect } from 'react';
import { useBreadcrumb } from 'react-router-mapping';

export default () => {

  const breadcrumb = useBreadcrumb();

  useEffect(() => {
    console.log(breadcrumb);
  }, []);
  ...
}

Return

[
  {
    "name": "home",
    "path": "/",
    "label": "Home",
  },
  {
    "name": "route-one",
    "path": "/route-one",
    "label": "Route One"
  },
  {
    "name": "sub-route-one",
    "path": "/route-one/sub-route-one",
    "label": "Sub Route One"
  }
]