Skip to content

Hook to manage filter values and keep them in sync with the url

Notifications You must be signed in to change notification settings

Drawbotics/drawbotics-use-filters

Repository files navigation

Use Filters

Hook to read/set filter values from/in the url.

Installation

$ npm i @drawbotics/use-filters

Usage

TypeScript

import { useFilters } from '@drawbotics/use-filters';

interface Filters {
  filterKey: string;
  filterKey2: 'one-value' | 'another-value';
}

// ... inside a component ...
const { filterKey, filterKey2 } = useFilter<Filters>(
  location,
  history,
  ['filterKey', 'filterKey2'],
  (key, value) => `Updated ${key} with ${value}`,
);

// read a filter value
console.log(filterKey.value); // => filterValue

// or an array value
console.log(filterKey.values); // => [filterValue]

// set a value
filterKey.set('newValue');

JavaScript

import { useFilters } from '@drawbotics/use-filters';

// ... inside a component ...
const { filterKey, filterKey2 } = useFilter(
  location,
  history,
  ['filterKey', 'filterKey2'],
  (key, value) => `Updated ${key} with ${value}`,
);

// read a filter value
console.log(filterKey.value); // => filterValue

// or an array value
console.log(filterKey.values); // => [filterValue]

// set a value
filterKey.set('newValue');

About

Hook to manage filter values and keep them in sync with the url

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •