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

use-ready-router

v1.0.1

Published

lightweight Next.js hook for safely accessing dynamic route parameters

Downloads

305

Readme

useReadyRouter

A lightweight Next.js hook for safely accessing dynamic route parameters.

It guards against undefined values during initial render or during a "hard-reload", making it ideal for use with data fetching libraries like SWR in Next.js applications.

Installation

Install the package from npm with your favorite package manager:

npm install use-ready-router
# or
yarn add use-ready-router
# or
pnpm add use-ready-router

Usage

The useReadyRouter hook takes a string parameter representing the name of the dynamic route parameter you want to access. This should match the name you've used in your Next.js dynamic route.

Basic Usage

import { useReadyRouter } from "use-ready-router/dist";

export default function CustomerPage() {
  const { value: customerId, isReady } = useReadyRouter("id");
  // Use customerId in your data fetching hook or component
}

With SWR

Ideally, you'll want to pass the value from your dynamic path to a query or custom data-hook, like the one below:

import { useReadyRouter } from "use-ready-router";
import { useCustomerPayments } from "@hooks/swr";

export default function CustomerPaymentsPage() {
  const { value: customerId } = useReadyRouter("id");
  const { data, isLoading } = useCustomerPayments(customerId);

  return (
    <AppLayout>
      <CustomerPayments transactions={data} loading={isLoading} />
    </AppLayout>
  );
}

Handling Loading State

You can use the isReady value to display a loading component if the router doesn't become ready on time:

import { CenterSpinner } from "@components/ui";
import { useReadyRouter } from "use-ready-router";

export default function CustomerPaymentsPage() {
  const { value: customerId, isReady } = useReadyRouter("id");

  if (!isReady) return <CenterSpinner containerHeight="100vh" />;

  return <AppLayout>{/* Your component logic here */}</AppLayout>;
}

Using a Custom Parser

The useReadyRouter hook accepts an optional second parameter: a custom parser function. This function allows you to transform the raw query parameter value into the desired type or format.

Basic Parser Example

import { useReadyRouter } from "use-ready-router";

export default function CustomerPage() {
  const { value: customerId } = useReadyRouter("id", (value) => Number(value));
  // customerId will be a number, or NaN if the conversion failed
}

Advanced Parser Example

You can use more complex parsing logic if needed:

import { useReadyRouter } from "use-ready-router";

const parseDateRange = (value: string | string[] | undefined) => {
  if (typeof value === "string") {
    const [start, end] = value.split(",");
    return {
      startDate: new Date(start),
      endDate: new Date(end),
    };
  }
  return { startDate: new Date(), endDate: new Date() }; // Default values
};

export default function ReportPage() {
  const { value: dateRange } = useReadyRouter("range", parseDateRange);
  // dateRange will be an object with startDate and endDate properties
}

Remember, the parser function should handle all possible input types (string | string[] | undefined) that can come from router.query.

TypeScript Support

useReadyRouter is written in TypeScript and provides full type safety. The return type of the hook will match the return type of your parser function, or default to string | string[] | undefined if no parser is provided.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.