{"id":15043467,"url":"https://github.com/jcoreio/react-router-transition-switch","last_synced_at":"2025-04-14T16:34:16.604Z","repository":{"id":25050837,"uuid":"103056397","full_name":"jcoreio/react-router-transition-switch","owner":"jcoreio","description":"a variant of \u003cSwitch\u003e that's much easier to use with transition components","archived":false,"fork":false,"pushed_at":"2023-11-14T01:42:27.000Z","size":1359,"stargazers_count":12,"open_issues_count":28,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-14T22:48:02.082Z","etag":null,"topics":["react-router","transitions"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/jcoreio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2017-09-10T19:37:18.000Z","updated_at":"2024-06-19T16:04:23.862Z","dependencies_parsed_at":"2024-06-19T16:03:53.941Z","dependency_job_id":"1305a841-1a20-4c93-8a98-df9a02bf40e2","html_url":"https://github.com/jcoreio/react-router-transition-switch","commit_stats":{"total_commits":189,"total_committers":4,"mean_commits":47.25,"dds":"0.12169312169312174","last_synced_commit":"e2be2be9e328a0d1d9bb83cadbdba110ed36ee44"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Freact-router-transition-switch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Freact-router-transition-switch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Freact-router-transition-switch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Freact-router-transition-switch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jcoreio","download_url":"https://codeload.github.com/jcoreio/react-router-transition-switch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248916925,"owners_count":21182893,"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-router","transitions"],"created_at":"2024-09-24T20:49:05.987Z","updated_at":"2025-04-14T16:34:16.580Z","avatar_url":"https://github.com/jcoreio.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-router-transition-switch\n\n[![CircleCI](https://circleci.com/gh/jcoreio/react-router-transition-switch.svg?style=svg)](https://circleci.com/gh/jcoreio/react-router-transition-switch)\n[![Coverage Status](https://codecov.io/gh/jcoreio/react-router-transition-switch/branch/master/graph/badge.svg)](https://codecov.io/gh/jcoreio/react-router-transition-switch)\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)\n[![npm version](https://badge.fury.io/js/react-router-transition-switch.svg)](https://badge.fury.io/js/react-router-transition-switch)\n\nThis is a variant of `\u003cSwitch\u003e` that's much easier to use with transition components and solves some problems.\n\nThe current recommended transition approach for `react-router` is\n\n```js\nimport { Route, Switch } from 'react-router-dom'\nimport Fader from 'react-fader'\n\nconst MyRoute = () =\u003e (\n  \u003cRoute\n    render={({ location }) =\u003e (\n      \u003cFader\u003e\n        \u003cSwitch key={location.key} location={location}\u003e\n          \u003cRoute path=\"/red\" component={Red} /\u003e\n          \u003cRoute path=\"/green\" component={Green} /\u003e\n          \u003cRoute path=\"/blue\" component={Blue} /\u003e\n        \u003c/Switch\u003e\n      \u003c/Fader\u003e\n    )}\n  /\u003e\n)\n```\n\nThis has several problems:\n\n1. All `\u003cSwitch\u003e`es transition on every `location` change, even if:\n   - only the last part of the URL changed and you only want the innermost nested `\u003cSwitch\u003e` to transition\n   - you have the same component for two different paths and don't want to transition that component\n   - you don't want to transition in some case for any other reason\n2. You have to pass a `location` to the `\u003cSwitch\u003e` for it to work\n\n`react-router-transition-switch` simplifies the above example to\n\n```js\nimport { Route } from 'react-router-dom'\nimport Switch from 'react-router-transition-switch'\nimport Fader from 'react-fader'\n\nconst MyRoute = () =\u003e (\n  \u003cSwitch component={Fader}\u003e\n    \u003cRoute path=\"/red\" component={Red} /\u003e\n    \u003cRoute path=\"/green\" component={Green} /\u003e\n    \u003cRoute path=\"/blue\" component={Blue} /\u003e\n  \u003c/Switch\u003e\n)\n```\n\n## Differences from `react-router`'s `\u003cSwitch\u003e`:\n\n1. You can pass it a `component` or `render` prop. It will use them to wrap the matched `\u003cRoute\u003e` if given\n2. By default it clones the matched `\u003cRoute\u003e` with `key={match.url}` unless you gave the `\u003cRoute\u003e` a key yourself.\n   This way the `Fader` will only perform a transition when:\n   - if you provide `key`s yourself, the matched `\u003cRoute\u003e` has a different `key` than the last\n   - otherwise, the _matched portion_ of the `location` is different from the last`\n3. You can pass it a `createKey` prop, which is a function taking the `(route, match)`\n   and returning the key to use.\n\n## `component` example\n\n```js\nimport React from 'react'\nimport { BrowserRouter as Router, Route } from 'react-router-dom'\nimport Fader from 'react-fader'\nimport Switch from 'react-router-transition-switch'\n\n// ...\nconst MyRoute = () =\u003e (\n  \u003cRouter\u003e\n    \u003cSwitch component={Fader}\u003e\n      \u003cRoute exact path=\"/\" component={Home} /\u003e\n      \u003cRoute path=\"/about\" component={About} /\u003e\n      \u003cRoute path=\"/account\" component={Account} /\u003e\n      \u003cRoute path=\"/users/:userId\" component={User} /\u003e\n    \u003c/Switch\u003e\n  \u003c/Router\u003e\n)\n```\n\nFor the location `/users/andy/profile`, the `\u003cSwitch\u003e` will render:\n\n```js\n\u003cFader\u003e\n  \u003cRoute key=\"/users/andy/profile\" path=\"/users/:userId\" component={User} /\u003e\n\u003c/Fader\u003e\n```\n\nNotice that it makes `match.url` the `key` of the matched `\u003cRoute\u003e`, so that `\u003cFader\u003e` (or whatever transition component\nyou use) knows to perform a transition. If you provide custom `key`s on the `\u003cRoute\u003e`s you pass to `\u003cSwitch\u003e`, it won't\noverwrite them.\n\n## `render` example\n\nAs with `\u003cRoute\u003e`, you may pass a `render` function instead of a `component`:\n\n```js\n\u003cRouter\u003e\n  \u003cSwitch\n    render={({ children }) =\u003e (\n      \u003cReactCSSTransitionGroup\n        transitionName=\"example\"\n        transitionEnterTimeout={300}\n        transitionLeaveTimeout={300}\n      \u003e\n        {children}\n      \u003c/ReactCSSTransitionGroup\u003e\n    )}\n  \u003e\n    ...\n  \u003c/Switch\u003e\n\u003c/Router\u003e\n```\n\n## Preventing transitions in certain cases\n\nIf you want to prevent transitions between certain `\u003cRoute\u003e`s, give them the same `key`. This will not cause problems\nbecause `\u003cSwitch\u003e` only renders one of the child `\u003cRoute\u003e`s it was passed, so there will never be duplicate keys during\nReact's reconciliation step.\n\n```js\n\u003cRouter\u003e\n  \u003cSwitch component={Fader}\u003e\n    \u003cRoute key=\"home\" exact path=\"/\" component={Home} /\u003e\n    \u003cRoute key=\"orders\" exact path=\"/orders\" component={Orders} /\u003e\n    \u003cRoute key=\"orders\" path=\"/orders/:orderId\" component={Orders} /\u003e\n    \u003cRoute key=\"about\" path=\"/about\" component={About} /\u003e\n  \u003c/Switch\u003e\n\u003c/Router\u003e\n```\n\n## Forcing transitions in certain cases\n\nIf you have to pass in an array of `\u003cRoute\u003e`s, they will already have\nkeys, hence changes between subroutes will not transition since\n`react-router-transition-switch` does not override existing keys with the\n`match.url`.\n\nIn this case, you can use the `createKey` prop to force a unique key for\nevery `match`:\n\n```js\n\u003cRouter\u003e\n  \u003cSwitch component={Fader} createKey={(child, match) =\u003e match.url}\u003e\n    {routes}\n  \u003c/Switch\u003e\n\u003c/Router\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcoreio%2Freact-router-transition-switch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjcoreio%2Freact-router-transition-switch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcoreio%2Freact-router-transition-switch/lists"}