{"id":26192909,"url":"https://github.com/dafunk-759/react-use-url","last_synced_at":"2026-04-11T15:37:56.058Z","repository":{"id":57347245,"uuid":"459925271","full_name":"Dafunk-759/react-use-url","owner":"Dafunk-759","description":"a tiny simple lib for react route.","archived":false,"fork":false,"pushed_at":"2022-02-23T06:56:55.000Z","size":1382,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-16T01:40:06.015Z","etag":null,"topics":["react","react-hook","react-router"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/Dafunk-759.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}},"created_at":"2022-02-16T09:00:15.000Z","updated_at":"2022-02-20T02:45:07.000Z","dependencies_parsed_at":"2022-09-05T08:20:56.127Z","dependency_job_id":null,"html_url":"https://github.com/Dafunk-759/react-use-url","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dafunk-759%2Freact-use-url","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dafunk-759%2Freact-use-url/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dafunk-759%2Freact-use-url/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dafunk-759%2Freact-use-url/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Dafunk-759","download_url":"https://codeload.github.com/Dafunk-759/react-use-url/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243137906,"owners_count":20242292,"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":["react","react-hook","react-router"],"created_at":"2025-03-12T01:25:10.297Z","updated_at":"2025-12-31T00:49:34.955Z","avatar_url":"https://github.com/Dafunk-759.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-use-url\n\n\u003e a tiny simple lib for react route\n\n## Install\n\n```bash\nnpm install --save react-use-url\n```\n\n## Usage\n\n```jsx\nimport React from \"react\"\nimport {\n  matchPath,\n  otherwise,\n  useUrl\n} from \"react-use-url\"\n\nexport default function App() {\n  const { path } = useUrl()\n\n  const body = matchPath(path, {\n    \"/\": () =\u003e \u003cHome /\u003e,\n    \"/teams\": () =\u003e \u003cTeams\u003e\u003cLeagueStandings /\u003e\u003c/Teams\u003e,\n    \"/teams/new\": () =\u003e \u003cTeams\u003e\u003cNewTeamForm /\u003e\u003c/Teams\u003e,\n    \"/teams/:teamId\": teamId =\u003e \u003cTeams\u003e\u003cTeam teamId={teamId} /\u003e\u003c/Teams\u003e,\n    [otherwise]: () =\u003e \u003cNotFound width={500} height={500} /\u003e\n  })\n\n  return (\n    \u003cMain\u003e\n      {body}\n    \u003c/Main\u003e\n  )\n}\n\nfunction Main({children}) {\n\n  return (\n    \u003cdiv style={outline(\"red\", 700, 700)}\u003e\n      Main\n      {children}\n    \u003c/div\u003e\n  )\n}\n\nfunction Home() {\n  return \u003cdiv style={outline(\"green\", 500, 500)}\u003e\n    Home\n  \u003c/div\u003e\n}\n\nfunction NotFound(width, height) {\n  return \u003cdiv style={outline(\"pink\", width, height)}\u003e\n    NotFound\n  \u003c/div\u003e\n}\n\nfunction Teams({children}) {\n  return (\n    \u003cdiv style={outline(\"blue\", 500, 500)}\u003e\n      Teams\n      {children}\n    \u003c/div\u003e\n  )\n}\n\nfunction Team({teamId}) {\n  return (\n    \u003cdiv style={outline(\"orange\", 300, 300)}\u003e\n      {\"teamId is: \" + teamId}\n    \u003c/div\u003e\n  )\n}\n\nfunction NewTeamForm() {\n  return \u003cdiv style={outline(\"black\", 300, 300)}\u003e\n    new team form\n  \u003c/div\u003e\n}\n\nfunction LeagueStandings() {\n  return \u003cdiv style={outline(\"yellow\", 300, 300)}\u003e\n    LeagueStandings\n  \u003c/div\u003e\n}\n\nfunction outline(color, width, height) {\n  return {\n    border: `2px solid ${color}`, \n    padding: \"20px\",\n    width: `${width}px`,\n    height: `${height}px`\n  }\n}\n\n```\n\n## Api\n\n### Url\n```ts\nexport interface Url {\n  path: string[],\n  hash: string,\n  search: string,\n  state?: string\n}\n```\n\n### useUrl\n`useUrl` is a React Hook to get `Url` and subscribe\nthe component's render to `watchUrl`.\n  \nwhen the `Url` change(`push` or `replace` get called).\nthe component will rerender to get the fresh data.\n\n``` ts\nexport function useUrl\u003cAssert = any\u003e(): Parsed\u003cUrl, Assert\u003e\n```\n\n### watchUrl\nstart watch url. When `push` or `replace` called.\nrun the `cb`.\n\n```ts\nexport function watchUrl(cb:WatchCB): WatchID\ntype WatchCB = (url:Url) =\u003e void\nexport type WatchID = () =\u003e void\n```\n\n### unwatchUrl\nstop watch url. use the given `watchID`.\n\n``` ts\nexport function unwatchUrl(watchID:WatchID): void\ntype WatchCB = (url:Url) =\u003e void\nexport type WatchID = () =\u003e void\n```\n\n### getInitUrl\nquery the global `location` and parse it to\n`Url`.\nthe object's state property is not parsed.\nwarning: you may get old data.\n\n```ts\nexport function getInitUrl(): Url\n```\n\n### push\nuse `history.pushState` to push the `path`\nand dispatch the `popstate` event.\n \nthis will trigger the subs rerun.\nthe state will be serialized and store in the `window.history.state`.\n`baseUrl` is default to \"\".\n```ts\nexport function push\u003cA\u003e(path:string, state?:A, baseUrl?:string): void\n```\n\n### replace\nuse `history.replaceState` to replace the `path`\nand dispatch the `popstate` event.\n\nthis will trigger the subs rerun.\nthe state will be serialized and store in the `window.history.state`.\n`baseUrl` is default to \"\".\n```ts\nexport function replace\u003cA\u003e(path:string, state?:A, baseUrl?:string): void\n```\n\n### matchPath\n`matchPath` is a simple helper to match your \nurl pattern.\n\n```ts\ntype F = (...args:string[]) =\u003e unknown\n\ntype MatchReturn\u003c\n  A extends Record\u003cstring|symbol, F\u003e\n\u003e = {\n  [k in keyof A]: ReturnType\u003cA[k]\u003e\n}[keyof A]\n\nexport function matchPath\u003c\n  A extends Record\u003cstring|symbol, F\u003e\n\u003e(path: string[], matchOption: A): MatchReturn\u003cA\u003e\n```\n\n### otherwise\n\n`otherwise` is used to catch all case.\n\nexample:\n```jsx\nmatchPath(path, {\n [otherwise]: () =\u003e \u003cNotFound /\u003e\n})\n```\n\n```ts\nexport const otherwise: unique symbol\n```\n\n## License\n\nMIT © [Dafunk-759](https://github.com/Dafunk-759)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdafunk-759%2Freact-use-url","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdafunk-759%2Freact-use-url","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdafunk-759%2Freact-use-url/lists"}