Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactCSSTransitionGroup only good with animating in new component?

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;
}
like image 593
Stefan Avatar asked Mar 07 '16 17:03

Stefan


People also ask

Is react good for animations?

React Transition Group is a good animation library and has a very small bundle size. It's one of the most popular animation libraries and should be considered for your next React project.

How do I use Csstransitiongroup?

All you need to do is put your array of items within the group to get started. You'll need an appear, appear active, enter, enter-active, leave, and leave-active. That's all you need!

How do you use CSSTransition react?

CSSTransition applies a pair of class names during the appear , enter , and exit states of the transition. The first class is applied and then a second *-active class in order to activate the CSS transition. After the transition, matching *-done class names are applied to persist the transition state.


2 Answers

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

like image 62
Stefan Avatar answered Sep 27 '22 17:09

Stefan


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

like image 22
Aditya Singh Avatar answered Sep 27 '22 16:09

Aditya Singh