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

router2

v4.0.4

Published

Client side routing for React applications. Inspired by Next.js.

Downloads

22

Readme

router2

Client side routing for React applications. Inspired by Next.js.

It supports history management, dynamic routing, and link creation.

This npm package provides TypeScript files, and it is your responsibility to build and bundle it.

This library is designed to be as simple as possible. It does not include any dependencies, making it easy to understand and modify. Additionally, this approach results in a smaller bundle size.

You can still fork the library and modify the package.json. For configuration details, see rollup.config.js.

To build the package, run:

npm run build

Update your package.json like this:

{
  "main": "dist/cjs/index.js",
  "module": "dist/esm/index.js"
}

Overview

const { navigate, route } = useRouter();

navigate({ pathname: "/new-route" });

console.log(route);
// {
//   path: "/new-route",
//   pathname: "/new-route",
//   params: {},
// }
const { navigate, route } = useRouter();

navigate(
  { pathname: `/dynamic-route/${12}`, query: { key: "value" } },
  { replace: true },
);

console.log(route);
// {
//   path: "/dynamic-route/12",
//   pathname: "/dynamic-route/:id",
//   params: { ":id": "12", key: "value" },
// }

Getting Started

npm install router2

Components

BrowserRouter

The BrowserRouter component is the main component of this library. It uses React Context to provide routing functionality to its children.

<BrowserRouter routes={routes} />

Properties

  • routes: This is a record object mapping route paths to React components. The "/404" path should be mapped to a component that will be displayed when no other route matches.

Example

Use routes object
const routes = {
  "/": () => <HomePage />,
  "/about": () => <AboutPage />,
  "/404": () => <NotFoundPage />,
};

<BrowserRouter routes={routes} />;
Use children
const routes = {
  "/": () => <HomePage />,
  "/about": () => <AboutPage />,
  "/404": () => <NotFoundPage />,
};

<BrowserRouter routes={routes}>
  {(Page) => (
    <InRouterContextProvider>
      <Page />
    </InRouterContextProvider>
  )}
</BrowserRouter>;

Link

The Link component creates an anchor element that interacts with the router.

<Link pathname="/some-route" query={{ key: "value" }} replace />

Properties

  • pathname: The path of the route to navigate to when the link is clicked.
  • query: An optional object representing the search parameters of the URL.
  • replace: An optional boolean indicating whether the current history entry should be replaced instead of creating a new entry.
  • All other properties of the anchor element are also supported.

API

useRouter

The useRouter hook returns the router object.

const router = useRouter();

Returns Router object.

Types

Router

This type represents a router object. It has the following properties:

  • path: The path of the current route.
  • navigate: A function to navigate to a different route. It takes a History object and an optional options object as parameters.
  • pathname: The path of the current location.
  • params: An object representing the URL parameters and search parameters.

History

This type represents a history entry. It has the following properties:

  • path: The path of the current route.
  • pathname: The pathname of the location.
  • query: An optional record object representing the search parameters of the URL.

Contributing

Feel free to contribute to this project by submitting issues and pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details.