{"id":19287953,"url":"https://github.com/ihoverlord/react-router-sample","last_synced_at":"2025-07-08T04:04:33.232Z","repository":{"id":138640212,"uuid":"421556791","full_name":"ihoverlord/react-router-sample","owner":"ihoverlord","description":"Sample React project to define routes with JSON config and redirecting users on 404","archived":false,"fork":false,"pushed_at":"2022-04-24T04:07:05.000Z","size":218,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-23T23:28:20.489Z","etag":null,"topics":["react","react-router","react-router-dom","reactjs"],"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/ihoverlord.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":"2021-10-26T19:23:23.000Z","updated_at":"2021-10-26T19:33:04.000Z","dependencies_parsed_at":null,"dependency_job_id":"366904fc-3cbc-48eb-9ede-00ffd642d1e1","html_url":"https://github.com/ihoverlord/react-router-sample","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ihoverlord/react-router-sample","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ihoverlord%2Freact-router-sample","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ihoverlord%2Freact-router-sample/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ihoverlord%2Freact-router-sample/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ihoverlord%2Freact-router-sample/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ihoverlord","download_url":"https://codeload.github.com/ihoverlord/react-router-sample/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ihoverlord%2Freact-router-sample/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264192231,"owners_count":23570737,"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-router","react-router-dom","reactjs"],"created_at":"2024-11-09T22:07:41.013Z","updated_at":"2025-07-08T04:04:33.197Z","avatar_url":"https://github.com/ihoverlord.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"### React Routing sample\n\n\nSample project to test defining routes with JSON and handle redirecting on 404\n\n1. Define routes in a JSON\n\n```\nconst routes = {\n      '/default': {\n         component: Home\n      },\n      '/dashboard': {\n         component: Dashboard\n      },\n      '/profile': {\n         component: Profile\n      }\n   }\n```\n\n2. Create a function to define routes dynamically\n\n```\nconst DefineRoutes = ({ routes }) =\u003e {\n   const paths = Object.keys(routes)\n   let location = useLocation();\n   // console.log({ location, has: routes[location.pathname] })\n   if (!routes.hasOwnProperty(location.pathname)) return \u003cRedirect to=\"/\" /\u003e\n   if (paths \u0026\u0026 paths.length \u003e 0) {\n      return (\n         \u003c\u003e\n            {paths.map(path =\u003e \u003cRoute path={path} key={path} component={routes[path].component} /\u003e)}\n         \u003c/\u003e\n      )\n   }\n\n}\n```\nThis function handles 404 and redirects the user to `/`\n\n3. Define router and import the routes\n\n```\n\u003cRouter\u003e\n \u003cSwitch\u003e\n  \u003cRoute exact path=\"/\" component={Home} /\u003e\n  \u003cDefineRoutes routes={routes} /\u003e\n \u003c/Switch\u003e\n\u003c/Router\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fihoverlord%2Freact-router-sample","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fihoverlord%2Freact-router-sample","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fihoverlord%2Freact-router-sample/lists"}