{"name":"use-url-search-params","version":"2.4.3","main":"dist/useUrlSearchParams.cjs.js","module":"dist/useUrlSearchParams.esm.js","browser":"dist/useUrlSearchParams.umd.js","typings":"dist/main.d.ts","author":{"name":"rudyhuynh","email":"rudyhuynh@"},"license":"MIT","peerDependencies":{"react":"*"},"repository":{"type":"git","url":"git+https://github.com/rudyhuynh/use-url-search-params.git"},"bugs":{"url":"https://github.com/rudyhuynh/use-url-search-params/issues"},"keywords":["react","react-hooks","urlsearchparams","url-query","url","state"],"scripts":{"build":"rm -rf dist && rollup -c","dev":"rollup -c -w","sync":"npx npm-link-watch ./dist && cd ./example && npx npm-link-watch use-url-search-params","restore-sync":"cd ./example && npx npm-unlink-watch use-url-search-params","test":"NODE_ENV=test jest --no-cache","test:watch":"NODE_ENV=test jest --no-cache --watch","test:coverage":"NODE_ENV=test jest --no-cache --coverage","eslint":"eslint ./src","build_publish":"yarn build && npm publish"},"devDependencies":{"@rollup/plugin-commonjs":"^21.0.1","@rollup/plugin-node-resolve":"^13.1.1","@rollup/plugin-typescript":"^8.3.0","@testing-library/react-hooks":"^3.2.1","babel-jest":"^26.0.1","esbuild":"^0.14.8","eslint":"^5.16.0","eslint-plugin-react":"^7.13.0","eslint-plugin-react-hooks":"^1.6.0","jest":"^26.0.1","jsdom":"^16.2.2","react":"^16.13.1","react-test-renderer":"^16.13.1","rollup":"^2.62.0","tslib":"^2.3.1","typescript":"^4.5.4"},"gitHead":"2b54b5cc9412c3a8d3245c3e395cdbcb4abe0dc8","description":"[![GitHub license](https://img.shields.io/github/license/Naereen/StrapDown.js.svg)](https://github.com/rudyhuynh/use-url-search-params/blob/master/License)","homepage":"https://github.com/rudyhuynh/use-url-search-params#readme","_id":"use-url-search-params@2.4.3","_nodeVersion":"14.18.1","_npmVersion":"7.24.2","dist":{"integrity":"sha512-CvL7UkePjsfl1mvK5QFNoeL41xNZhUNBo0QFwPwXhxIeUaQzUllszZ5NIw6Zi5gc4a/qBuEOsdSYVrtzoGnuDQ==","shasum":"7594998949901a06ebc70f8104b77d79d217af4c","tarball":"http://registry-npm.ywwl.com/use-url-search-params/download/use-url-search-params-2.4.3.tgz","fileCount":15,"unpackedSize":43676,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhyVelCRA9TVsSAnZWagAAJnwP/jpdcglnDXXN8Lqsq9J9\nrs39MKYY1n61uBrWb2ek1JdkqtB6Oo/XW7kJy6jcaGp5iW2HLflchTr6k7ZO\n6SuC+Hn542hAigWExZW0XkOZCXqZXqvyP4T/uykXEXfRPHVH7ZaCma3vz4f+\ncsHFUQQifDJeUoKzUDT75NhO3Y3WRsQm4g0TKtLb5sZmW1nukqRlJqJMH09m\nUbdg1j4ks1zFocDWP2TLNwFKRTYMyEwGb6DLzycDrXV0zQeWX3YXhgo1BGvl\nkFNV44gT0n7xgbqf9/nxYUd100Zz4q/fOQyouW76fX3d8s804brB2fFnJyG4\n0lGn50++KKCkPBN6MFh5JeXmomBcVCp7yxZ6Zlq0m/rn+B3Z1DfpvG/8XAPF\n2WcGaLPEX4/qNdVIIS+Yz0ePPVZegiUUdAkJYyo7tQDY7qtBKPTlj9P2g9ZJ\nGPLqwHmm2O65wAdjrMhdVTSUUag1r6YTZtfWKZTWxZfoIKJNkoGeL68GBlAT\n9vxN1A+G2AybMC/vzFWY4spoN3KMx5pHNUzbsik16AsC1X/XO9Q8v0MAZuuX\npxhm0uvPMtX7T6crN8z2jcTttXKV+7pVBKPYiD5iKZYmnsw9hqYRPGz3pYnB\n0m6KA+uweiOG76nL8ydKGxlIZoHWvB9bwQb6iSOHljFVB3e/SeXVYqze/jMN\nElUT\r\n=wdd7\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCOEZ66VPrxt8w3u1r9J4NzlMWT3IYXOioOiLvSq5h87QIhAKliVmD9tWPhaIWL7qLlc0P8mhNAjDx0HlRVS5ThUxAY"}],"size":9109,"noattachment":false,"key":"/use-url-search-params/-/use-url-search-params-2.4.3.tgz"},"_npmUser":{"name":"rudyhuynh","email":"rudy.huynh286@gmail.com"},"directories":{},"maintainers":[{"name":"rudyhuynh","email":"rudy.huynh286@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/use-url-search-params_2.4.3_1640585125550_0.5321223165026545"},"_hasShrinkwrap":false,"readme":"# `useUrlSearchParams()`\n\n[![GitHub license](https://img.shields.io/github/license/Naereen/StrapDown.js.svg)](https://github.com/rudyhuynh/use-url-search-params/blob/master/License)\n\nA React Hook to use [URL query string](https://en.wikipedia.org/wiki/Query_string) as a state management\n\n[Demo](https://rudyhuynh.github.io/use-url-search-params)\n\n## Why you need this\n\n- Your app need to persist its state after user refresh the page (used for simple, non-sensitive data).\n- Some page settings (ex: table filter, sorting, paging, etc.) should be saved in the URL so that user can easily pass to others. e.g. Tester can easily send a URL of a page to developer with very least reproduce steps.\n- You want to do something (request new data, etc.) every time some URL query value changes.\n- Combine all of the above with a URL query as a single source of truth.\n\n## Installation\n\n```\nnpm install use-url-search-params\n```\n\nor\n\n```\nyarn add use-url-search-params\n```\n\n## How to use\n\nFor most of the time you will do something like this:\n\n```js\nimport React from \"react\";\nimport { useUrlSearchParams } from \"use-url-search-params\";\n\nfunction App() {\n  // Your page URL will be like this by default: http://my.page?checked=true\n  const [params, setParams] = useUrlSearchParams({ checked: true });\n\n  React.useEffect(() => {\n    // do something when `params.checked` is updated.\n  }, [params.checked]);\n\n  return (\n    <div>\n      <input type=\"checkbox\" checked={params.checked} onChange={(e) => setParams({ checked: e.target.checked })} />\n    </div>\n  );\n}\n```\n\n## How to control the value parsed from URL query\n\nBy default, all values parsed from URL query are string. In case you want to get boolean or number value, pass a second argument to `useUrlSearchParams()` to specify data type you want to get from `params` object. Here is an example:\n\n```js\nconst initial = {\n  y: \"option1\",\n};\nconst types = {\n  x: Number,\n  y: Boolean,\n  z: Date,\n  t: [\"option1\", \"option2\", \"option3\"],\n};\nconst [params, setParams] = useUrlSearchParams(initial, types);\n\n// `params.x` will be number (or NaN)\n// `params.y` will be one of [undefined, true, false]\n// `params.z` will be instance of Date (can be Invalid Date)\n// `params.t` will be one of [\"option1\", \"option2\", \"option3\"] (can be `undefined` if not specified in `initial`)\n```\n\n## Complex data structure\n\nAlthough you can use `JSON.parse()` and `JSON.stringify()` to get/set arbitrary serializable data to URL query, it is not recommended. URL query is a good place to store and persist page settings as key/value pairs such as table filter, sorting, paging, etc. We should keep it that way for simplicity. **For complex data structure, you should consider using other state management for better performance, security and flexibility.**\n\n> **WARNING**: Be aware of XSS attack. Be careful to validate values from URL query before using it by either using `types` - the second parameter passed to `useUrlSearchParams()` or validate them yourself if neccessary.\n\nBut if you still insist, here is an example:\n\n```js\nfunction App() {\n  const [params, setParams] = useUrlSearchParams(\n    {},\n    {\n      complexData: (dataString) => {\n        try {\n          return JSON.parse(dataString);\n        } catch (e) {\n          return {};\n        }\n      },\n    }\n  );\n\n  const onSetParams = (data) => {\n    setParams({ complexData: JSON.stringify(data) });\n  };\n\n  return <div>{/*...*/}</div>;\n}\n```\n\n## React Router\n\nShould just work with React Router or any routing system. Just make sure that your component re-render whenever route changes.\n\n## API\n\n- **useUrlSearchParams([initial, types, replace])**\n  - `initial` (optional | Object): To set default values for URL query string.\n  - `types` (optional | Object): Has similar shape with `initial`, help to resolve values from URL query string. Supported types:\n    - `String` (default)\n    - `Number`\n    - `Bool`\n    - `Date` - [`Date​.prototype​.toISOString()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) is used to parse date to string, e.g date string in your URL query is zero UTC offset\n    - Array of available string values (like enum)\n    - A custom resolver function\n  - `replace` (optional | boolean | default: false): If true, will call `histor#replaceState()` instead of `history#pushState()` on url search param change.\n\n## Read more (for maintainers)\n\nThis library is built base on [URLSearchParams interface](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)\n\n## License\n\nMIT\n","publish_time":1640585125708,"_cnpm_publish_time":1640585125708,"dist-tags":{"latest":"2.5.1"}}