Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pojo-router

Package Overview
Dependencies
Maintainers
4
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pojo-router

A react hooks library to associate metadata to a path

  • 0.2.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
4
Created
Source

pojo-router

npm downloads bundle size npm version PRs Welcome

A hook based machanism to convert a string (i.e. a path) into metadata.

API

Define your metadata in a provider component.

import React from 'react';
import PojoRouter from 'pojo-router';

const namedPaths = {
  page1: '/page1',
  page2: { path: '/page2', sensitive: true },
  page3: { path: '/page3/:id' },
};

const routes = [
  ['page1', { abc: 123 }],
  ['page2', { abc: 456 }],
  ['page3', { abc: 789 }],
  ['/page4', { abc: 0 }]
];

const notFound = { nothing: true };

const Router = ({ children }) => (
  <PojoRouter namedPaths={namedPaths} routes={routes} notFound={notFound}>
    { children }
  </PojoRouter>
);

Within your child component, use one of the hooks.

useCurrentPath

Sets or gets the "current path". This sets will use this path for all calls to useCurrentMatch until a new path is set.

If a path is provided, it sets that path.

It always returns the current path.

useMatches

Given a path, this returns all the metadata for routes that match.

useFirstMatch

Given a path, this returns the metadata for the first route that matches.

useBestMatch

Given a path and a compareFunction (see Array.prototype.sort), this returns the metadata for the best route that matches based on the provided compare function.

useCurrentMatch

When a path is set via useCurrentPath, this returns the first match metadata for the current path that is set.

useOutboundRoute

Given a named route (or route string if none is defined), this returns a function that will generate a matching string, including populating the dynamic variables. E.g. for a route like /entity/:id an outbound routing function like entityPath({ id: 123 }) will generate /entity/123.

Keywords

FAQs

Package last updated on 28 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc