1.0.8 • Published 11 months ago

use-url-parameters v1.0.8

Weekly downloads
-
License
ISC
Repository
github
Last release
11 months ago

useURLParameters

An intuitive react hook that plugs URL Parameters into your react hook lifecycle.

Installation

With yarn

yarn add use-url-parameters

With npm

npm install use-url-parameters

With pnpm

pnpm install use-url-parameters

How it Works

On Component Render

  • Read URL Parameters with the name of each key passed into the hook's first parameter
  • Sets your components state based the current URL parameters using each function passed in as the second argument

On State Update

  • Updates your URL parameters to match the state passed in as the hooks first parameter

Example

import { useState } from "react";
import useURLParameters from "use-url-parameters";

export default function Component() {
  const [filters, setFilters] = useState({
    name: "",
    age: "",
  });

  useURLParameters(
    {
      name: filters.name,
      age: filters.age,
    },
    {
      name: (val) => setFilters((prev) => ({ ...prev, name: val })),
      age: (val) => {
        // Only set the age if the URL Parameter is a number
        if (!isNaN(parseInt(val))) {
          setFilters((prev) => ({ ...prev, age: val }));
        }
      },
    }
  );

  ...
}
1.0.8

11 months ago

1.0.7

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago