🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

use-query-params

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-query-params

React Hook for managing state in URL query parameters with easy serialization.

2.2.1
latest
Version published
Weekly downloads
332K
-5.7%
Maintainers
1
Weekly downloads
 
Created

What is use-query-params?

The `use-query-params` package is a React hook for managing URL query parameters in a declarative way. It simplifies the process of reading and updating query parameters in the URL, making it easier to manage state that is reflected in the URL.

What are use-query-params's main functionalities?

Reading Query Parameters

This feature allows you to read query parameters from the URL. In this example, the `search` query parameter is read and displayed in the component.

import { useQueryParam, StringParam } from 'use-query-params';

function MyComponent() {
  const [search, setSearch] = useQueryParam('search', StringParam);
  return <div>Search: {search}</div>;
}

Updating Query Parameters

This feature allows you to update query parameters in the URL. In this example, the `search` query parameter is updated based on the input field's value.

import { useQueryParam, StringParam } from 'use-query-params';

function MyComponent() {
  const [search, setSearch] = useQueryParam('search', StringParam);
  return (
    <div>
      <input value={search || ''} onChange={e => setSearch(e.target.value)} />
    </div>
  );
}

Handling Multiple Query Parameters

This feature allows you to handle multiple query parameters simultaneously. In this example, both `search` and `page` query parameters are managed and updated.

import { useQueryParams, StringParam, NumberParam } from 'use-query-params';

function MyComponent() {
  const [query, setQuery] = useQueryParams({ search: StringParam, page: NumberParam });
  return (
    <div>
      <input value={query.search || ''} onChange={e => setQuery({ search: e.target.value })} />
      <button onClick={() => setQuery({ page: (query.page || 0) + 1 })}>Next Page</button>
    </div>
  );
}

Other packages similar to use-query-params

FAQs

Package last updated on 24 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts