{"id":18982283,"url":"https://github.com/profusion/use-location-query-state","last_synced_at":"2026-06-25T11:31:32.977Z","repository":{"id":63369003,"uuid":"515348146","full_name":"profusion/use-location-query-state","owner":"profusion","description":null,"archived":false,"fork":false,"pushed_at":"2023-05-11T11:43:31.000Z","size":91,"stargazers_count":0,"open_issues_count":2,"forks_count":1,"subscribers_count":19,"default_branch":"main","last_synced_at":"2025-02-14T10:43:37.667Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/profusion.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2022-07-18T21:35:25.000Z","updated_at":"2022-10-03T17:57:19.000Z","dependencies_parsed_at":"2024-10-30T03:54:31.549Z","dependency_job_id":null,"html_url":"https://github.com/profusion/use-location-query-state","commit_stats":{"total_commits":13,"total_committers":4,"mean_commits":3.25,"dds":0.3846153846153846,"last_synced_commit":"f2dee4d0050474ac3e6970e82621941ab4a6a603"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/profusion%2Fuse-location-query-state","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/profusion%2Fuse-location-query-state/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/profusion%2Fuse-location-query-state/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/profusion%2Fuse-location-query-state/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/profusion","download_url":"https://codeload.github.com/profusion/use-location-query-state/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239986930,"owners_count":19729705,"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":[],"created_at":"2024-11-08T16:13:02.312Z","updated_at":"2025-11-11T11:02:14.534Z","avatar_url":"https://github.com/profusion.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![developed by](https://img.shields.io/badge/developed%20by-profusion-blue)\n![npm bundle size](https://img.shields.io/bundlephobia/min/@profusion/use-location-query-state)\n![npm](https://img.shields.io/npm/v/@profusion/use-location-query-state)\n\n# @profusion/use-location-query-state\n\nThis is a React hook which wraps `React.useState` and `location.search` in a single hook named as `useLocationQueryState`.\n\nUsing this hook, you will have the same `[state, setState]` return, but the hook will internally keep your state \"in sync\" with the page query string.\n\nWhen called for the first time, the hook will check for the query string and return its values as initial state, optionally you can also pass a initial state as a \"fallback\", that will be used when there is no querystring at the page URL.\n\nFinally, when you call a `setState`, the page URL will be updated for you.\n\nThis way, you can easily have a state, for example, for your pagination, such as `{ page: 5, limit: 10 }` and this state will be reflected in the page URL as `?page=5\u0026limit=10`. With this your users can refresh the page, bookmark, or share a link to the exact location and state they want to share.\n\n## Installing\n\n```\nnpm i @profusion/use-location-query-state\n// or with yarn\nyarn add @profusion/use-location-query-state\n```\n\n## Basic Usage\n\n```tsx\nconst INITIAL_STATE = { _page: 0, _limit: 10 };\n\nconst FancyComponent = () =\u003e {\n  const [state, setState] = useLocationQueryState(INITIAL_STATE);\n\n  const handleNextPage = useCallback(() =\u003e {\n    setState(currentState =\u003e ({\n      ...currentState,\n      _page: currentState._page + 1\n    }))\n  }, []);\n\n  return (\n    \u003cdiv\u003e\n      Showing page: {state._page}\n\n      \u003cbutton onClick={handleNextPage}\u003eNext page\u003c/button\u003e\n    \u003c/div\u003e\n  )\n}\n```\n\n## Using custom converters\n\nYou can customize how your data is encoded/decoded to be stored/retrieved from the page URL.\n\nTo control how your state is converted, you can pass a second argument to the hook with your encode and decode functions.\n\n```ts\nuseLocationQueryState(\n  INITIAL_QUERY_STATE,\n  {\n    decode: (value) =\u003e Object.fromEntries(value.entries()),\n    encode: (value) =\u003e new URLSearchParams(value),\n  }\n);\n```\n\nYour `decode` function receives a `URLSearchParams` and should return it as a plain javascript object.\n\nWhile your encoder receives your state object and should return it parsed as `URLSearchParams`.\n\n### Field-level converters\n\nYou can also set custom converters at field level. In the example above, if you want to \"store\" your `_page` in the URL as a binary number, and convert it back to decimal when retrieving from URL, you can set a custom converter to this field:\n\n```ts\nimport createFieldBasedConverter from '@profusion/use-location-query-state/converters/createFieldBasedConverter';\n\nconst converter = createFieldBasedConverter({\n  _page: {\n    decode: (value, fieldName) =\u003e parseInt(value, 2),\n    encode: (value, fieldName) =\u003e value.toString(2),\n  },\n});\n\nconst INITIAL_STATE = { _page: 0, _limit: 10 };\n\nfunction FancyComponent() {\n  const [state, setState] = useLocationQueryState(INITIAL_STATE, converter);\n}\n```\n\nThis `createFieldBasedConverter` receives an object where each key represents a field in your state.\n\nIf you don't specify a custom converter for a field, the default converter will be used (`JSON.stringify` and `JSON.parse`). So, in the example above, you won't lose the `_limit` field.\n\nAt the field level, your converters will receive the field name as second argument.\n\n### Full example\n\nWe provide a full React application as an example of this library usage.\n\nTake a look at our [GitHub repository](github), in `examples/ts-react-router6` directory.\n\nTo run it, clone the repository, go to the example app directory and run `yarn \u0026\u0026 yarn start`.\n\nOpen your browser at the URL given by the start command and play around. :smile:\n\n## Building it\n\n1. At the library dir, install dependencies and peer-dependencies:\n\n```\nyarn\nyarn install-peers\n```\n\n2. Then build\n\n```\nyarn build\n```\n\nWe provide both commonjs and esmodules distributions, so you will see these two different distributions under `dist/` directory.\n\n```\ndist\n├── cjs\n└── esm\n```\n\n[github]: https://github.com/profusion/use-location-query-state\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprofusion%2Fuse-location-query-state","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprofusion%2Fuse-location-query-state","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprofusion%2Fuse-location-query-state/lists"}