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-route-type

v4.3.5

Published

Type-safe helpers for React-Router

Downloads

169

Readme

react-route-type / Exports

react-route-type

A collection of types and utility functions to facilitate typesafe routing in react-router-dom and react-router-native.

npm

npm i react-route-type

yarn

yarn add react-route-type

Usage

import { route } from "react-route-type";

export const Routes = {
  home: route("home"),
  view: route("view"),
  details: route(["view", ":id"]),
  users: route("users", { query: { search: "" } }),
};

const viewDetailsTemplate = Routes.details.template(); // -> /view/:id
const viewDetailsCreate = Routes.details.create({ id: "2" }); // -> /view/2

const viewDetailsCreateERROR = Routes.details.create({}); // ERROR: property 'id' is missing in type {}

// Usage with React Router
import { Route, Switch } from "react-router-dom";
import { Home, Summary, Details } from "./components";
export class App extends React.PureComponent {
  render() {
    <Switch>
      <Route path={Routes.home.template()} component={Home} />
      <Route path={Routes.view.template()} component={Summary} />
      <Route path={Routes.details.template()} component={Details} />
    </Switch>;
  }
}

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

export class Home extends React.PureComponent {
  render() {
    <div>
      <h1>Welcome Home!</h1>
      <Link to={Routes.details.create({ id: "3" })} />
      <Link to={Routes.view.create({})} />
    </div>;
  }
}

Hooks

useParams

Params is required

export const Routes = {
  details: route(["view", ":id"]),
};

// "/view/:id"
<Route path={Routes.details.template()} component={Details} />;

export const Details = () => {
  const { id } = Routes.details.useParams();
};

useQueryParam

All property of query is optional

export const View = () => {
  const { search } = Routes.view.useQueryParam();
};

With Default value

const users = route(["users"], {
  query: { withDefault: "default" },
});

export const Users = () => {
  const { withDefault } = Routes.view.useQueryParam();

  /// withDefault === "default" is true
};

Nested routes

const home = route("home");
const settings = route("settings").createNestedRoutes((parent)=>({
   global: parent.route("global");
   advanced: parent.route("advanced");
}));

// App.js
function App() {
  return (
    <Routes>
      <Route
        path={home.template()} // "/home"
        element={<Home />}
      />
      <Route
        path={settings.root.template()} // "/settings/*"
        element={<Settings />}
      />
    </Routes>
  );
}

// Settings.js
function Settings() {
  return (
    <Container>
      <Conversations />

      <Routes>
        <Route
          path={setting.global.template()} // "global"
          element={<Global />}
        />
        <Route
          path={setting.advanced.template()} // "advanced"
          element={<Advanced />}
        />
      </Routes>
    </Container>
  );
}

useMap

This is useful for create breadcrumb

const routeMap = setting.advanced.useMap(); // [{path:"settings",create=()=>"/settings"},{path:"advanced",create=()=>"/settings/advanced"}]

return (
  //antd
  <Breadcrumb>
    {routeMap.map(({ path, create }) => {
      <Breadcrumb.Item key={path}>
        <a href={create()}></a>
      </Breadcrumb.Item>;
    })}
  </Breadcrumb>
);

useCreate

This is useFull for where you have a dynamic param

const routes = route("lang").createNestedRoutes((parent)=>({
  about: parent.route("about");
   price: parent.route("price");
}));
// current /en/about
const createPrice = routes.root.price.useCreate();

return (
  <Link to={
    createPrice() // /en/price
  }>link to Price</Link>
);