Open cannon303 opened 3 years ago
@cannon303 Here's a codesandbox example that should be helpful!
Essentially, each Route
needs it's own CSSTransition
, and each CSSTransition
needs a nodeRef
for the individual route.
////// CONTENT TRANSITION ROUTER
const PageContent = withRouter(({ location }) => {
let routeRefs: any[] = [];
const isMatch = useCallback(
(path: string): boolean => {
return location.pathname === path ?? false;
},
[location]
);
return (
<>
{appRoutes.map(({ path, Component }, index) => {
routeRefs[index] = React.useRef(null);
return (
<Route key={index} exact path={path}>
{() => {
// Route callback ensures the transitions are loaded correctly
return (
<CSSTransition
nodeRef={routeRefs[index]}
in={isMatch(path)}
timeout={300}
classNames="fade"
unmountOnExit
appear
>
<div ref={routeRefs[index]} className="fade">
<Component />
</div>
</CSSTransition>
);
}}
</Route>
);
})}
</>
);
});
I am using react-transition-group v4.4.1.
Here is what I did and worked.
const { location } = this.props; // use with withRouter
const transitionKey = location.pathname;
<div className={classes.root}>
<TransitionGroup component={null}>
<CSSTransition
key={transitionKey}
classNames="slideLeftToRight"
addEndListener={(node, done) => node.addEventListener("transitionend", done, false)}
>
<Switch location={location}>
<Route path="/">
<div id="page 1" />
</Route>
<Route path="/another">
<div id="page 2" />
</Route>
</Switch>
</CSSTransition>
</TransitionGroup>
</div>
TransitionGroup looks at the transition key, and if there is a change in pathname, TransitionGroup will execute the transitioning between components.
Hey Jordan! I love your SandBox. I have a very specific use case that needs HashRouter, and I am using v6. I am trying to play with your code and get ur demo working in my case, but I am having difficulty getting the out transition to work. Is there any reason you can see this adaptation wouldn't work?
function StackNavigator() {
const location = useLocation();
const routeRefs = useRef<any>([]);
const currentKey = location.pathname.split("/")[1] || "/";
const isMatch = useCallback(
(path: string): boolean => {
return location.pathname === path ?? false;
},
[location],
);
return (
<Routes>
{tabRoutes.map(({ route, Element }, index) => {
return (
<Route
key={route}
path={route}
element={
<CSSTransition
key={currentKey}
nodeRef={routeRefs.current[index]}
in={isMatch(route)}
timeout={300}
classNames="fade"
unmountOnExit
appear
>
<div
ref={(ref) => (routeRefs.current[index] = ref)}
style={{ height: "100%", width: "100%" }}
className="fade"
>
<Element />
</div>
</CSSTransition>
}
></Route>
);
})}
</Routes>
);
}
It does seem as though the refs aren't having an effect, But I don't know why that would be.
Hi I am hoping for a bit of help here if possible. I cannot get transition group to work. The routing works fine but there are no transitions and the console.log("FIRED") isn't outputted either. I believe the Switch component isn't supported here and that maybe what is causing the problem? However I cannot find the right documentation to tell me what to change it to. Here is a simple example where specific components fade in and out at the same time when a link is clicked. I also get an error in strict mode in the console which I'll post at the bottom. Can anyone help me please?
**I'm a bit stuck as React is something I'm new to and looking at other examples I cannot see how they relate to my project as they look so different. Here is the error in console:
Warning: Legacy context API has been detected within a strict-mode tree.
The old API will be supported in all 16.x releases, but applications using it should migrate to the new version.
Please update the following components: Transition, TransitionGroup
Learn more about this warning here: https://reactjs.org/link/legacy-context TransitionGroup@http://localhost:3000/static/js/vendors~main.chunk.js:328926:30 Route@http://localhost:3000/static/js/vendors~main.chunk.js:324374:29 div div TransGroup@http://localhost:3000/static/js/main.chunk.js:1450:88 Router@http://localhost:3000/static/js/vendors~main.chunk.js:324005:30 HashRouter@http://localhost:3000/static/js/vendors~main.chunk.js:323573:35 index.js:1**
Anyone know where I'm going wrong please?