{"id":14977653,"url":"https://github.com/victortrusov/react-router-loading","last_synced_at":"2025-08-20T12:31:04.480Z","repository":{"id":39642731,"uuid":"228907117","full_name":"victortrusov/react-router-loading","owner":"victortrusov","description":"Wrapper for react-router that allows you to load data before switching the screen","archived":false,"fork":false,"pushed_at":"2023-11-07T04:07:31.000Z","size":3917,"stargazers_count":64,"open_issues_count":4,"forks_count":14,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-12-16T07:05:23.978Z","etag":null,"topics":["bar","fetch","load","loading","prefetch","preloading","react","react-router","react-router-dom","router","switching","topbar"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/victortrusov.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":"2019-12-18T19:24:13.000Z","updated_at":"2024-11-09T10:02:49.000Z","dependencies_parsed_at":"2024-06-19T19:14:50.091Z","dependency_job_id":null,"html_url":"https://github.com/victortrusov/react-router-loading","commit_stats":{"total_commits":68,"total_committers":6,"mean_commits":"11.333333333333334","dds":0.4411764705882353,"last_synced_commit":"de5c1449e8c740e7c09cc997be5bb56a0c911313"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/victortrusov%2Freact-router-loading","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/victortrusov%2Freact-router-loading/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/victortrusov%2Freact-router-loading/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/victortrusov%2Freact-router-loading/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/victortrusov","download_url":"https://codeload.github.com/victortrusov/react-router-loading/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230423559,"owners_count":18223435,"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":["bar","fetch","load","loading","prefetch","preloading","react","react-router","react-router-dom","router","switching","topbar"],"created_at":"2024-09-24T13:56:05.041Z","updated_at":"2024-12-19T11:11:31.077Z","avatar_url":"https://github.com/victortrusov.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-router-loading · [![npm version](https://badge.fury.io/js/react-router-loading.svg)](https://badge.fury.io/js/react-router-loading)\n\nWrapper for `react-router` that allows you to load data before switching the screen  \n\\\n![](example.gif)\\\n\u003ca href=\"https://codesandbox.io/s/react-router-6-loading-demo-q7iv97\" target=\"_blank\"\u003eDEMO\u003c/a\u003e (React Router 6)  \n\u003ca href=\"https://codesandbox.io/s/react-router-loading-demo-sguvm\" target=\"_blank\"\u003eDEMO 0.x.x\u003c/a\u003e (React Router 5)\n\n## Requirements\n### ‼️ Version `1.x.x` supports React Router 6 only, please use version `0.x.x` for React Router 5 ‼️\n\n|  |  |  |\n| ------------ | ------- | --- |\n| react        | \u003e= 16.8 |     |\n| react-router | **^5.0.0** |  **Package version 0.x.x**  |\n| react-router | **^6.0.0** |  **Package version 1.x.x**  |\n\nThis package uses `react-router` (`react-router-dom` or `react-router-native`) as main router so you should implement it in your project first.\n\n## Installation\n\n```console\nnpm install react-router-loading\n## or\nyarn add react-router-loading\n```\n# Usage\n## React Router 6 (package version 1.x.x)\n\nIn your router section import `Routes` and `Route` from `react-router-loading` instead of `react-router-dom` or `react-router-native`\n```js\nimport { Routes, Route } from \"react-router-loading\";\n\n\u003cRoutes\u003e\n    \u003cRoute path=\"/page1\" element={\u003cPage1 /\u003e} /\u003e\n    \u003cRoute path=\"/page2\" element={\u003cPage2 /\u003e} /\u003e\n    ...\n\u003c/Routes\u003e\n```\n\nAdd `loading` prop to every route that needs to be loaded before switching\n```js\n\u003cRoutes\u003e\n    // data will be loaded before switching\n    \u003cRoute path=\"/page1\" element={\u003cPage1 /\u003e} loading /\u003e\n\n    // instant switch as before\n    \u003cRoute path=\"/page2\" element={\u003cPage2 /\u003e} /\u003e\n    ...\n\u003c/Routes\u003e\n```\n\nAdd `loadingContext.done()` at the end of your initial loading method in components that mentioned in routes with `loading` prop (in this case it's `Page1`)\n```js\nimport { useLoadingContext } from \"react-router-loading\";\nconst loadingContext = useLoadingContext();\n\nconst loading = async () =\u003e {\n    // loading some data\n\n    // call method to indicate that loading is done and we are ready to switch\n    loadingContext.done();\n};\n```\n\n## React Router 5 (package version 0.x.x)\n\nIn your router section import `Switch` and `Route` from `react-router-loading` instead of `react-router-dom`\n```js\nimport { Switch, Route } from \"react-router-loading\";\n\n\u003cSwitch\u003e\n    \u003cRoute path=\"/page1\" component={Page1} /\u003e\n    \u003cRoute path=\"/page2\" component={Page2} /\u003e\n    ...\n\u003c/Switch\u003e\n```\n\nAdd `loading` prop to every route that needs to be loaded before switching\n```js\n\u003cSwitch\u003e\n    // data will be loaded before switching\n    \u003cRoute path=\"/page1\" component={Page1} loading /\u003e\n\n    // instant switch as before\n    \u003cRoute path=\"/page2\" component={Page2} /\u003e\n    ...\n\u003c/Switch\u003e\n```\n\nAdd `loadingContext.done()` at the end of your initial loading method in components that mentioned in routes with `loading` prop (in this case it's `Page1`)\n```js\nimport { LoadingContext } from \"react-router-loading\";\nconst loadingContext = useContext(LoadingContext);\n\nconst loading = async () =\u003e {\n    // loading some data\n\n    // call method to indicate that loading is done and we are ready to switch\n    loadingContext.done();\n};\n```\n## Class components\n```js\nimport { LoadingContext } from \"react-router-loading\";\n\nclass ClassComponent extends React.Component {\n    ...\n    loading = async () =\u003e {\n        // loading some data\n\n        // call method from props to indicate that loading is done\n        this.props.loadingContext.done();\n    };\n    ...\n};\n\n// we should wrap class component with Context Provider to get access to loading methods\nconst ClassComponentWrapper = (props) =\u003e\n    \u003cLoadingContext.Consumer\u003e\n        {loadingContext =\u003e \u003cClassComponent loadingContext={loadingContext} {...props} /\u003e}\n    \u003c/LoadingContext.Consumer\u003e\n\n```\n\n## Config\n\nYou can specify loading screen that will be shown at the first loading of your app\n```js\nconst MyLoadingScreen = () =\u003e \u003cdiv\u003eLoading...\u003c/div\u003e\n\n\u003cRoutes loadingScreen={MyLoadingScreen}\u003e // or \u003cSwitch\u003e\n...\n\u003c/Routes\u003e\n```\n\nUse `maxLoadingTime` property if you want to limit loading time. Pages will switch if loading takes more time than specified in this property (ms).\n```js\n\n\u003cRoutes maxLoadingTime={500}\u003e // or \u003cSwitch\u003e\n...\n\u003c/Routes\u003e\n```\n\nIf you want to change LoadingContext globally you can pass `isLoading` property to the `\u003cRoutes /\u003e` or `\u003cSwitch /\u003e`. This way you don't need to add extra `loadingContext.done();` in your page components after fetching is done.\n```js\nimport { useIsFetching } from 'react-query';\nconst isFetching = useIsFetching();\n\n\u003cRoutes isLoading={isFetching}\u003e // or \u003cSwitch\u003e\n...\n\u003c/Routes\u003e\n```\n\nCall `topbar.config()` if you want to change topbar configuration. More info \u003ca href=\"http://buunguyen.github.io/topbar/\" target=\"_blank\"\u003ehere\u003c/a\u003e.\n```js\nimport { topbar } from \"react-router-loading\";\n\ntopbar.config({\n    autoRun: false,\n    barThickness: 5,\n    barColors: {\n        0: 'rgba(26,  188, 156, .7)',\n        .3: 'rgba(41,  128, 185, .7)',\n        1.0: 'rgba(231, 76,  60,  .7)'\n    },\n    shadowBlur: 5,\n    shadowColor: 'red',\n    className: 'topbar'\n});\n```\n# Development\n\nClone repository and run\n```sh\n# go to lib folder\ncd packages/react-router-loading\n\n# restore packages\nyarn\n\n# build lib\nyarn build\n\n# go to example folder\ncd ../../examples/react-router-6\n\n# restore packages\nyarn\n\n# run example\nyarn dev\n```\n\nrun `yarn build` in lib folder each time you want to apply changes\n\n## License\n\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvictortrusov%2Freact-router-loading","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvictortrusov%2Freact-router-loading","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvictortrusov%2Freact-router-loading/lists"}