Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can React animate a component being hidden/removed?

Tags:

I know that using ReactCSSTransitionGroup you can apply it to a list of items and have then animate as they appear or disappear. What about a single component though?

See JSFiddle here, where I've got a CSS animation when the item appears. But I don't know if there's a way to make it animate upon being hidden.

https://jsfiddle.net/eq263rk2/

like image 702
ffxsam Avatar asked Sep 19 '15 21:09

ffxsam


People also ask

How do you animate mount and unmount of a react component?

We do this (same with animation): import Animated from "react-mount-animation"; const MyComponent = () => { const [isMounted, setIsMounted] = useState(false); ... return ( <Animated. div //You can use any HTML element here show={isMounted} mountAnim={` 0% {opacity: 0} 100% {opacity: 1} `} > Hi World!

How do you slide or hide a div with transition in react?

You need to give . App a height of 100% and your #root div that renders your entire React App needs a set height. You also need to change the transition from using height to using max-height .

How do you hide components in react?

Hence, props can also be used to hide or show a component. For example, if we pass props, based on the props value, we can hide or show the elements, just like in the below example. Here in this example, the props we have declared is called showHideDemo1 along with the state value.


1 Answers

Sure! Just use the enter and leave variants (though you should always render the TransitionGroup component):

var TheThing = React.createClass({
    render() {
        var component;
        if (this.props.visible) {
            component = <div className="the-thing">The Thing</div>;
        }

        return (
            <ReactCSSTransitionGroup transitionName="thing">
                {component}
            </ReactCSSTransitionGroup>
        )
    }
});

and

.thing-enter {
  opacity: 0.01;
  transition: opacity 1s ease-in;
}

.thing-enter.thing-enter-active {
  opacity: 1;
}

.thing-leave {
  opacity: 1;
  transition: opacity 1s ease-in;
}

.thing-leave.thing-leave-active {
  opacity: 0.01;
}

Example: https://jsfiddle.net/BinaryMuse/3fkso0kq/

like image 125
Michelle Tilley Avatar answered Sep 23 '22 11:09

Michelle Tilley