1.0.8 • Published 1 year ago

use-url-parameters v1.0.8

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year 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

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago