{"id":18632327,"url":"https://github.com/frejs/use-routes","last_synced_at":"2025-04-11T07:31:04.201Z","repository":{"id":43016306,"uuid":"176284606","full_name":"frejs/use-routes","owner":"frejs","description":":triangular_flag_on_post: A router hook for not only react hooks API but also fre","archived":false,"fork":false,"pushed_at":"2023-01-03T18:17:54.000Z","size":180,"stargazers_count":34,"open_issues_count":10,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-05-05T09:02:32.733Z","etag":null,"topics":["react","react-rooter","userouter","vue-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/frejs.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":"2019-03-18T12:50:17.000Z","updated_at":"2023-08-31T07:54:49.000Z","dependencies_parsed_at":"2023-02-01T08:46:09.754Z","dependency_job_id":null,"html_url":"https://github.com/frejs/use-routes","commit_stats":null,"previous_names":["132yse/fre-router"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frejs%2Fuse-routes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frejs%2Fuse-routes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frejs%2Fuse-routes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frejs%2Fuse-routes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/frejs","download_url":"https://codeload.github.com/frejs/use-routes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223460646,"owners_count":17148760,"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-rooter","userouter","vue-router"],"created_at":"2024-11-07T05:11:45.784Z","updated_at":"2024-11-07T05:11:46.361Z","avatar_url":"https://github.com/frejs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cimg align=\"right\" height=\"150\" src=\"https://ws1.sinaimg.cn/large/0065Zy9egy1g189q52z05j30dw0dwwgo.jpg\" /\u003e\n\n# use-routes\n\n\u003e 800 Bytes router hook for Fre and React\n\n[![](https://img.shields.io/npm/v/use-routes.svg?style=flat)](https://npmjs.com/package/use-routes)\n[![](https://img.shields.io/npm/dm/use-routes.svg?style=flat)](https://npmjs.com/package/use-routes)\n[![](https://img.shields.io/bundlephobia/minzip/use-routes.svg?style=flat)](https://bundlephobia.com/result?p=use-routes)\n\n### Use\n\n```javascript\nimport { h, render } from 'fre'\nimport { useRoutes, push } from 'use-routes'\n\nconst routes = {\n  '/': () =\u003e (\n    \u003cdiv\u003e\n      \u003cp\u003ehome\u003c/p\u003e\n      \u003cbutton onClick={() =\u003e push('/home/jack')}\u003eGo jack\u003c/button\u003e\n    \u003c/div\u003e\n  ),\n  '/home/:id': ({ id }) =\u003e (\n    \u003cdiv\u003e\n      \u003cp\u003e{id}\u003c/p\u003e\n      \u003cbutton onClick={() =\u003e push('/')}\u003eGo home\u003c/button\u003e\n    \u003c/div\u003e\n  ),\n}\n\nconst App = () =\u003e useRoutes(routes)\n\nrender(\u003cApp /\u003e, document.getElementById('root'))\n```\n\n以上，首先定义一个路由的对象，key 为正则路径，value 为组件\n\n只 `history` 模式\n\n### React\n\nuse-routes 同时适用于 fre 和 react，但默认是服务于 fre 的\n\n如果想要用于 react，可以通过 webpack 配置别名\n\n```js\nresolve: {\n  alias: {\n    'fre': 'react'\n  }\n}\n```\n```js\nimport React from 'react'\nimport { useRoutes } from 'use-routes'\n\nfunction App(){\n  const result = useRoutes(routes)\n  return result || \u003cNotFound /\u003e\n}\n```\n\n#### useRoutes\n\n```JSX\nfunction App(){\n  const result = useRoutes(routes)\n  return result || \u003cNotFound /\u003e\n}\n```\n\n`useRoutes` 返回一个组件，这个组件会根据 `pathname` 和 routes 的 key 匹配\n\n如果匹配不到，可以事先准备一个全局的 404 组件 √\n\n#### redirect\n\n重定向，只需要将 routes 对象的 value 变成需要重定向的 路径字符串 即可\n\n```jsx\nconst routes = {\n  '/': '/home',\n  '/home': Home,\n}\n```\n\n如上，当匹配到跟路径，会跳转到 home 路径，并渲染 home 组件\n\n#### A link\n\n方便跳转，默认也对外暴露了一个 A 组件，作用和 Link 组件一样：\n\n```JSX\n\u003cA href='/home/jack'\u003eGo jack\u003c/A\u003e\n```\n\n标签属性和 a 标签一样，只是默认做了阻止冒泡\n\n#### License\n\n_MIT ©132yse, inspired by routerhook_\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrejs%2Fuse-routes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrejs%2Fuse-routes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrejs%2Fuse-routes/lists"}