I'd like to animate between two components where the first component fades out and is removed from the DOM before the next component is added to the DOM and fades in. Otherwise, the new component is added to the DOM and takes up space before the old component is removed. You can see the problem in this fiddle:
http://jsfiddle.net/phepyezx/4
// css snippet
.switch-enter {
opacity: 0.01;
}
.switch-enter.switch-enter-active {
opacity: 1.0;
}
.switch-leave {
opacity: 1.0;
}
.switch-leave.switch-leave-active {
opacity: 0;
}
// React snippet
<ReactCSSTransitionGroup transitionName="switch">
<div key={key} className={className}>{this.text()}</div>
</ReactCSSTransitionGroup>
An unacceptable solution (for me) is to hide the original component with css before transitioning to the new component as seen here:
http://jsfiddle.net/phepyezx/5
// Change to css
.switch-leave {
visibility: hidden;
height: 0px;
width: 0px;
opacity: 1.0;
}
Is there a way to "delay" react from mounting a new component before the original is removed? I'm open to velocity or some other library to achieve this.
Thanks
Solved using the
componentWillUnmount()
lifecycle method.http://jsfiddle.net/phepyezx/9/
Here's the code:
And the relevant css:
You can achieve the same effective result with Jonny Buchanan's answer which uses absolute positioning and a delay instead of
componentWillUnmount()
Another solution is to make the incoming and outgoing elements take up the same space, for example by having them both absolutely positioned:
http://jsfiddle.net/phepyezx/7/
You can use
transition-delay
to wait until the leaving component disappears before making the entering component appear, e.g.:If you want to delay the rendering of the next component, you could use something like this:
And in your render method: