{"id":16483555,"url":"https://github.com/trevorblades/use-query-string","last_synced_at":"2025-03-16T18:31:43.201Z","repository":{"id":35027837,"uuid":"197438850","full_name":"trevorblades/use-query-string","owner":"trevorblades","description":"🆙 A React hook that serializes state into the URL query string","archived":false,"fork":false,"pushed_at":"2023-03-04T04:21:19.000Z","size":1024,"stargazers_count":55,"open_issues_count":6,"forks_count":5,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-30T05:27:45.301Z","etag":null,"topics":["hooks","query-string","querystring","react","react-hooks","serialization","state-management"],"latest_commit_sha":null,"homepage":"https://npm.im/use-query-string","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/trevorblades.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-17T18:03:25.000Z","updated_at":"2023-09-08T20:39:21.000Z","dependencies_parsed_at":"2024-06-18T22:54:15.076Z","dependency_job_id":null,"html_url":"https://github.com/trevorblades/use-query-string","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trevorblades%2Fuse-query-string","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trevorblades%2Fuse-query-string/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trevorblades%2Fuse-query-string/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trevorblades%2Fuse-query-string/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trevorblades","download_url":"https://codeload.github.com/trevorblades/use-query-string/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243401497,"owners_count":20285055,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hooks","query-string","querystring","react","react-hooks","serialization","state-management"],"created_at":"2024-10-11T13:14:23.777Z","updated_at":"2025-03-16T18:31:42.409Z","avatar_url":"https://github.com/trevorblades.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# useQueryString\n\n[![Build Status](https://github.com/trevorblades/use-query-string/workflows/Node%20CI/badge.svg)](https://github.com/trevorblades/use-query-string/actions)\n\nA React hook that serializes state into the URL query string\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [Configuration](#configuration)\n  - [`parseOptions`](#parseoptions)\n  - [`stringifyOptions`](#stringifyoptions)\n- [Examples](#examples)\n  - [Gatsby example](#gatsby-example)\n  - [Practical example](#practical-example)\n  - [An example using context](#an-example-using-context)\n- [License](#license)\n\n## Installation\n\n```bash\n$ npm install use-query-string\n```\n\n## Usage\n\nGiven a location object and a history updater function, this hook will return an array who's first element is an object representing the current URL query string. The second element in the array is a function that serializes an object into the query string and updates the former `query` object.\n\n```js\nimport useQueryString from 'use-query-string';\n\nconst [query, setQuery] = useQueryString(location, updateQuery);\n```\n\nThe first argument passed to the hook is a [`Location`](https://developer.mozilla.org/en-US/docs/Web/API/Location) object, and the second is a history-updating function with the following signature:\n\n```ts\n(path: string): void =\u003e {\n  // update the browser history\n}\n```\n\n## Configuration\n\n### `parseOptions`\n\nYou can supply an optional third argument to the hook that gets passed along as options to the `parse` function. These allow you to do things like automatically convert values to numbers or booleans, when appropriate. See [the `query-string` docs](https://github.com/sindresorhus/query-string#parsestring-options) for all of the accepted options.\n\n```js\nconst [query, setQuery] = useQueryString(\n  location,\n  navigate,\n  {\n    parseNumbers: true,\n    parseBooleans: true\n  }\n);\n```\n\n### `stringifyOptions`\n\nYou can also pass a fourth argument to the hook that gets used as options for the `stringify` function that serializes your state. This is especially useful if you need to serialize/deserialize arrays a way other than the default. See [the `query-string` docs](https://github.com/sindresorhus/query-string#stringifyobject-options) for all of the accepted options.\n\n```js\nconst arrayFormat = 'comma';\nconst [query, setQuery] = useQueryString(\n  location,\n  navigate,\n  {arrayFormat},\n  {\n    skipNull: true,\n    arrayFormat\n  }\n);\n```\n\n## Examples\n\nIn this example, you'll see a component using the query string to serialize some state about a selected color. The component uses the global [`Location`](https://developer.mozilla.org/en-US/docs/Web/API/Location) object, and a function that calls [`History.pushState`](https://developer.mozilla.org/en-US/docs/Web/API/History/pushState) to update the page URL.\n\n```jsx\nimport React from 'react';\nimport useQueryString from 'use-query-string';\n\nfunction updateQuery(path) {\n  window.history.pushState(null, document.title, path);\n}\n\nfunction ColorPicker() {\n  const [{color}, setQuery] = useQueryString(\n    window.location,\n    updateQuery\n  );\n\n  function handleColorChange(event) {\n    setQuery({color: event.target.value});\n  }\n\n  return (\n    \u003cdiv\u003e\n      \u003cp style={{color}}\u003eColor is {color}\u003c/p\u003e\n      \u003cselect value={color} onChange={handleColorChange}\u003e\n        \u003coption value=\"red\"\u003eRed\u003c/option\u003e\n        \u003coption value=\"blue\"\u003eBlue\u003c/option\u003e\n      \u003c/select\u003e\n    \u003c/div\u003e\n  );\n}\n```\n\n### Gatsby example\n\nIf you're using Gatsby, you could pass `props.location` and the `navigate` helper function from [Gatsby Link](https://www.gatsbyjs.org/docs/gatsby-link/) as arguments to the hook.\n\n```js\n// pages/index.js\nimport React from 'react';\nimport useQueryString from 'use-query-string';\nimport {navigate} from 'gatsby';\n\nfunction Home(props) {\n  const [query, setQuery] = useQueryString(\n    props.location, // pages are given a location object via props\n    navigate\n  );\n\n  // ...the rest of your page\n}\n```\n\n### Practical example\n\nThe following CodeSandbox contains an example for working with multiple boolean filters that change something in the page and persist between reloads.\n\n[![Edit zen-stallman-6r908](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/zen-stallman-6r908?fontsize=14\u0026hidenavigation=1\u0026theme=dark)\n\n### An example using context\n\nWhen building a complex app, you may have multiple components within a page that need to read from and write to the query string. In these cases, using a `useQueryString` hook in each component will cause your query string to fall out of sync, since each invocation of the hook [manages its own internal state](./src/index.ts#L14).\n\nTo avoid this issue, use **context** to pass `query` and `setQuery` to descendant components within a page.\n\n```js\n// src/pages/billing.js\nimport React, {createContext, useContext} from 'react';\nimport useQueryString from 'use-query-string';\nimport {navigate} from 'gatsby';\n\n// create context to use in parent and child components\nconst QueryStringContext = createContext();\n\nexport default function Billing(props) {\n  const [query, setQuery] = useQueryString(props.location, navigate);\n  return (\n    \u003cQueryStringContext.Provider value={{query, setQuery}}\u003e\n      \u003cdiv\u003e\n        \u003cFilterInput name=\"client\" /\u003e\n        \u003cFilterInput name=\"industry\" /\u003e\n        \u003cFilterInput name=\"email\" /\u003e\n      \u003c/div\u003e\n      {/* render table of filtered data */}\n    \u003c/QueryStringContext.Provider\u003e\n  );\n}\n\nfunction FilterInput(props) {\n  const {query, setQuery} = useContext(QueryStringContext);\n\n  function handleChange(event) {\n    const {name, value} = event.target;\n    setQuery({[name]: value});\n  }\n\n  return (\n    \u003cinput\n      name={props.name}\n      value={query[props.name]}\n      onChange={handleChange}\n    /\u003e\n  );\n}\n```\n\n[![Edit Nested components example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/nested-components-example-fyed0?fontsize=14\u0026hidenavigation=1\u0026theme=dark)\n\n## License\n\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrevorblades%2Fuse-query-string","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrevorblades%2Fuse-query-string","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrevorblades%2Fuse-query-string/lists"}