-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[v6] animated switch between routes in v6 #7297
Comments
I think two things need to happen:
The latter is optional, but more scalable as an API. Then adding a location prop to |
This sounds good to me. For me it would be fine to only add it to the |
Related pull request: #7298 |
Can someone explain the advantages of adding location in Routes with a usage example? I personally don't use 3rd-part transition/animation library, and can do animated page transition using useNavigate, useEffects and css. In the example below, I set a timeout of 500ms in an effect, and a 500ms opacity transition when clicking on a button. export default function Welcome() {
const navigate = useNavigate();
const [path, setPath] = useState<string | null>(null);
function handleClick(path: string) {
setPath(path);
}
useEffect(() => {
if (path) setTimeout(() => navigate(path), 500);
});
return (
<div
className={path ? cn(container, "transition-opacity", "duration-500", "ease-linear", "opacity-0") : container}
>
<Button
onClick={() => handleClick("/discover/")}
className={pinkButton}
disabled={path !== null}
>
<span className="m-auto">Discover</span>
</Button> I'd like to find a similar approach that would be declarative, though. |
@AdrienLemaire Well, within a |
@MeiKatz thanks for the clarification, makes sense in order to show parts of both pages at the same time during the animation. Another thought: Wouldn't the useDeferredValue hook from upcoming concurrent mode also help with that, providing an alternative to the Routes location solution? I have not yet managed to get it working in my case with the experimental branches. |
@AdrienLemaire The Edit: I thought a little bit further: I don't think that function AnimatedRoutes({
children,
classNames,
timeout,
...rest
}) {
const location = useLocation();
return (
<TransitionGroup {...rest}>
<CSSTransition
key={location.key}
timeout={timeout}
classNames={classNames}
>
<Routes location={location}>
{children}
</Routes>
</CSSTransition>
</TransitionGroup>
);
} You see? No need for |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
This issue is still fresh even so there is a pending pull request. |
Possible alternative approach with current release without location on Routes, depending how complex your use case... I have always done these routes transitions using a
In their example for v5, they just map over the routes (no parent Switch) and use the function child variant of Route to decide if the child route component is rendered or not based on the match. See below: <Router>
...
{routes.map(({ path, Component }) => (
<Route key={path} exact path={path}>
{({ match }) => (
<CSSTransition
in={match != null}
timeout={300}
classNames="page"
unmountOnExit
>
<div className="page">
<Component />
</div>
</CSSTransition>
)}
</Route>
))}
...
</Router> From what I can tell, v6's
const MyRoute = ({ path, element }) => {
const resolvedPath = useResolvedPath(path);
const match = useMatch(resolvedPath.pathname);
return (
<CSSTransition
in={match != null}
timeout={300}
classNames="page"
unmountOnExit
>
<div>
{element}
</div>
</CSSTransition>
);
}
<BrowserRouter history={history}>
...
{routes.map(({ path, element }) => (
<MyRoute key={path} path={path} element={element} />
))}
...
</BrowserRouter> The reason this works is because all I have a feeling that Right now, I'm using this in one of my nested route outlets for a specialised case. Seems to be doing what I expect. |
@woollsta You're right, but the approach I realised in my referenced PR makes it a lot easier. Currently the problem is, that @mjackson and @ryanflorence are busy doing other stuff and therefore RRv6 makes no progress at all. Hopefully this will change in the near future. Would be a pity if not. |
This is likely a stupid question, but is there a way to reference the patched branch in my project so I can try it out prior to it's eventual merge? I gave it a go, but I think bc of the way the monorep is set up I'm not able to reference the packages inside. E.g. tried |
You can clone my repository and run the install script.. |
in my project, I didn't use I also use I haven't tried with v6 yet. But hope that there will be another workaround if the |
I've just come across this attempting to pair react-spring with React Router v6. Example with v5 (linked from react-spring): https://codesandbox.io/s/jp1wr1867w - old component leaves, new component enters Example with v6: https://codesandbox.io/s/react-router-spring-b5941 - old component is immediately replaced, then animates out and back in @MeiKatz could you explain what changes, if any, I'd need to get this working with your PR? |
@jymbob in RRv6 there is no |
@MeiKatz I understand that's true as of now. Would your PR bring back this ability, or have I misunderstood? |
@jymbob Definitely! The only reason why this does not work, is that @ryanflorence and @mjackson are currently working on their main project and have no time for RR. Actually, that is kind of a really bad situation right now. |
I'm running into the same issue, thank you for addressing it @MeiKatz. I hope your fix is merged soon. |
@Meenaaaa I hope so, I hope so. If there is no fix until end of 2021 we should think about a fork of RR. |
Looking forward to seeing you champion the fork @MeiKatz <3 |
Duplicate of #7117, we'll share updates there. |
Are there any plans on how somebody could realise animated transitions between routes? In v5 we could use the
location
prop of the<Switch />
component, but in the new<Routes />
component there isn't any replacement for this. Or is there an other way to do it in v6?The text was updated successfully, but these errors were encountered: