0.9.0 • Published 2 years ago

@better-typed/react-query-params-hooks v0.9.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

React Query Params Hooks

NPM npm bundle size npm type definitions NPM npm GitHub stars

Easy query params handling for React

Features

  • :rocket: Simple, fast and light
  • :factory: Set, Update, Delete, Clear
  • 🪗 Provider for global configs and any environment

Install

npm install --save @better-typed/react-query-params-hooks

or

yarn add @better-typed/react-query-params-hooks

Usage

import React from "react";
import { useQueryParams } from "@better-typed/react-query-params-hooks";

const MyComponent: React.FC = () => {
  const {queryParams, queryString, stringify, setQueryParam, setQueryParams, updateQueryParams, deleteQueryParam, clearQueryParams } = useQueryParams()

  return (
    // ...
  )
}

Connect to your router

First we have to wrap our App with QueryParamsProvider and provide setQueryParams and getQueryString methods. Below we have an example of usage with React Router v6.

import React from "react";
import { useQueryParams } from "@better-typed/react-query-params-hooks";

const App: React.FC = ({ children }) => {
  const location = useLocation();
  const [, setSearchParams] = useSearchParams();

  return (
    <QueryParamsProvider
      getQueryString={() => location.search}
      setQueryParams={(search) => setSearchParams({ search }, { replace: true })}
    >
      {children}
    </QueryParamsProvider>
  );
};