简体   繁体   English

React Animation无法卸载组件

[英]React Animation not working on unmount of component

I'm working on a component that renders another element that I would like to animate its entrance and exit.On mount and unmount, I would like for this component to fade it's opacity, respective to the action it corresponds with. 我正在一个组件上渲染另一个我想为其入口和出口设置动画的元素。 Right now, I have this basic setup, and I'm unable to trigger a fade out. 现在,我已经有了基本设置,并且无法触发淡出。 Am I wrapping my component in ReactCSSTransitionGroup in the wrong place? 我是否将组件包装在ReactCSSTransitionGroup中的错误位置?

Code: 码:

import ReactCSSTransitionGroup from 'react/lib/ReactCSSTransitionGroup;

class Parent extends React.Component {
  static defaultProps = {
    exampleArr = ['one', 'two', 'three']
  };

  render() {
    <div>
      {exampleArr ?
        {this.props.exampleArr.map((child) => {
          return <ReactCSSTransitionGroup
            transitionName="example"
            transitionAppear={true}
            transitionLeave={true}
            transitionEnterTimeout={600}
            transitionLeaveTimeout={600}
          >
           <Child key={}/>
          </ReactCSSTransitionGroup>
         });
         : null
    </div>
  }
}

My CSS is as follows: 我的CSS如下:

.example-enter {
  opacity: 0.01;
 }

.example-enter.example-enter-active {
  opacity: 1;
  transition: opacity 600ms ease-in;
 }

 .example-leave {
   opacity: 1;
 }

 .example-leave.example-leave-active {
   opacity: 0.01;
   transition: opacity 600ms ease-in;
 }

Does anybody have any insight as to why it's fading in on mount, but just popping away entirely without any animation on unmount? 是否有人对为什么它在挂载中逐渐消失有任何见解,而只是在弹出时没有任何动画就完全消失了?

Currently, you are wrapping each array item of your exampleArr in a transition group. 当前,您正在将exampleArr每个数组项包装在过渡组中。 Instead, wrap the entirety of your generated elements in a single transition group like this: 而是将整个生成的元素包装在单个过渡组中,如下所示:

import React, { Component, cloneElement } from 'react;
import ReactCSSTransitionGroup from 'react-addons-css-transition-group';

class Parent extends Component {
  render() {
    <div>
      <ReactCSSTransitionGroup
        transitionName="example"
        transitionAppear={true}
        transitionAppearTimeout={600}
        transitionEnterTimeout={600}
        transitionLeaveTimeout={600}
      >
        {this.props.children}
      </ReactCSSTransitionGroup>
    </div>
  }
}

Then, use your parent element as a wrapper around the children like this: 然后,将您的父元素用作子元素的包装,如下所示:

<Parent>
  <div>one</div>
  <Child>two</Child>
  <AnyOtherComponent>three</AnyOtherComponent>
</Parent>

If you create the elements inside your parent class as in your example, replace {this.props.children} with: 如果像示例中那样在父类内部创建元素,请将{this.props.children}替换为:

{this.props.exampleArr.map(item => <div>{item}</div>)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM