I am trying to find a working example of low level animations using the React Router v4 with React Transition Group v2. I have looked at the example on the React Router docs but they only use CSS Animation with one route.
This is how I currently use the React Router:
export const App = () => (
<div className="app-container">
<main className="app-container__content">
<Switch>
<Route exact path="/projects/:slug" component={ProjectPage} />
<Route exact path="/" component={StartPage} />
</Switch>
</main>
</div>
);
And this is my Root.jsx:
const Root = ({ store, history }) => (
<Provider store={store}>
<ConnectedRouter history={history}>
<Switch>
<Route path="/" component={App} />
</Switch>
</ConnectedRouter>
</Provider>
);
I have tested the solution here: https://hackernoon.com/animated-page-transitions-with-react-router-4-reacttransitiongroup-and-animated-1ca17bd97a1a - but it doesn't work. Could some one point me in the right direction?
Update
I have tried like this, but the callback doesn't get called. So I end up with to pages in the dom.
export const App = ({ location }) => {
console.log(location);
return (
<div className="app-container">
<main className="app-container__content">
<ScrollToTop />
<TransitionGroup>
<Switch key={location.pathname} location={location}>
<Route exact path="/projects/:slug" component={ProjectPage} />
<Route exact path="/" component={StartPage} />
</Switch>
</TransitionGroup>
</main>
</div>
);
};
you are missing the
Transition
component itselfit should look something like this:
notice that the key is on the
CSSTransition
itself and not on theSwitch
Update
If you want to implement it by yourself here is a basic implementation of
CSSTransition
you get
this.props.in
fromTransitionGroup
to indicate whether you are entering or leaving.this.props.onExited
is another prop that you get fromTransitionGroup
, you must call it when the exit animation completes, soTransitionGroup
will know that you should be unmounted.