ReactCSSTransitionGroup only good with animating in new component?
Asked Answered
T

2

10

I have ReactCSSTransitionGroup working fine (I think), the newly mounted component fades in in all its glory.

The problem is that the component being replace just abruptly disappears, a problem for me because eventually I would like that my components transition in and out of the viewport...

I don't seem to see any way of telling the leaving component how to disappear elegantly.

Am I missing something or is ReactCSSTransitionGroup only capable of animating the incoming component?

UPDATE

Code below:

import React from "react";
import ReactDOM from "react-dom";
import { Router, Route, IndexRoute, hashHistory, Link } from "react-router";
import ReactCSSTransitionGroup from "react-addons-css-transition-group";


class PageOne extends React.Component {
  render() {
    return (
      <ReactCSSTransitionGroup transitionName="example" transitionAppear={true} transitionAppearTimeout={500} transitionEnterTimeout={500} transitionLeaveTimeout={500}>
        <div>
          HI FROM PAGE ONE<br />
          <br />
          <Link to="two">Take me to Page Two</Link>
        </div>
      </ReactCSSTransitionGroup>
    );
  }
}
class PageTwo extends React.Component {
  render() {
    return (
      <ReactCSSTransitionGroup transitionName="example" transitionAppear={true} transitionAppearTimeout={500} transitionEnterTimeout={500} transitionLeaveTimeout={500}>
        <div>
          HELLO FROM PAGE TWO<br />
          <br />
          <Link to="/">Go back to Page One</Link>
        </div>
      </ReactCSSTransitionGroup>
    );
  }
}


const app = document.getElementById('app');
ReactDOM.render(
  <Router history={hashHistory}>
    <Route path="/" component={PageOne}></Route>
    <Route path="two" component={PageTwo}></Route>
  </Router>,
app);

And the CSS:

.example-enter {
  opacity: 0.01;
}
.example-enter.example-enter-active {
  opacity: 1;
  transition: opacity 500ms ease-in;
}
.example-leave {
  opacity: 1;
}
.example-leave.example-leave-active {
  opacity: 0.01;
  transition: opacity 300ms ease-in;
}
Terrilynterrine answered 7/3, 2016 at 17:40 Comment(2)
Explanation on react transitions explains that you can have transitions on appear, enter and also leave. If you need further help here, could you share some code?Edina
Updated the post with bare minimum code to show what I am going for. The above code does not fade in/out on component mount/unmount.Terrilynterrine
T
4

Ok it looks like they had it worked out already and I just didn't discover it until now.

The React-Router git repo has a list of examples and within is one for "Animation". This demo shows how you can leverage ReactCSSTransitionGroup for animating in/out "Pages" by cloning the element and assigning a key.

https://reacttraining.com/react-router/web/example/animated-transitions

Terrilynterrine answered 8/3, 2016 at 22:14 Comment(0)
O
2

I have created a bin which is working fine for components being mounted/ unmounted. Take a look and let me know if it id similar to what you are doing and still not able to solve the problem

Osculum answered 7/3, 2016 at 18:53 Comment(7)
This may be my fault in trying to describe the problem. What I am actually trying to achieve is a transition between pages using ReactCSSTransitionGroup.Terrilynterrine
Another question might be, is ReactCSSTransitionGroup something that should be used for page transitions to begin with?Terrilynterrine
I took the code down to the bare minimum, please look above to see what I am referring to. The components do not fade in/out on mount/unmount. Is ReactCSSTransitionGroup only good for transitioning things such as list items? Do I need to try and handle pages like items in a list?Terrilynterrine
The best explained use case of ReactCSSTransitionGroup is adding/removing elements from list because we can track the element entering or leaving the DOM in a list scenario. Refer: 1ReactCSSTransitionGroup is based on ReactTransitionGroup and is an easy way to perform CSS transitions and animations when a React component enters or leaves the DOM.` So if you apply it to a page transitioning in and out of DOM you need to out the transition on the parent of the view container where the views are being rendered. Adding it on link, as in your example, won't workOsculum
As @AdityaSingh points out, ReactCSSTransitionGroup does not cover animations on page changes (as in react router). If you want to apply transitions on navigating routes, you may want to check out this solution which overrides <Location> component to achieve page transitions.Edina
Interesting, thank you guys for the help. Thanks @winvelt, I had previously discovered that and didnt switch to it as I was already using react-router since it seems like the more "approved" solution but I will now focus on playing with it.Terrilynterrine
So I attempted implementing those two and it worked ok but introduced another set of problem. I went back to digging and landed on a demo section for React-Router and it turns out they had it solved all along. View my answer below.Terrilynterrine

© 2022 - 2024 — McMap. All rights reserved.