@better-typed/react-query-params-hooks
v0.9.0
Published
React query params hooks
Downloads
8
Readme
React Query Params Hooks
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>
);
};