简体   繁体   English

react-transition-group生命周期方法componentWillLeave不被调用

[英]react-transition-group lifecycle method componentWillLeave not being called

I'm trying to get componentWillLeave to get called and I'm having no luck. 我正在尝试让componentWillLeave被调用,但我没有运气。 componentWillAppear gets called, but I'm unable to get the former to call. componentWillAppear被调用,但是我无法让前者调用。 Most responses I've seen call for making sure the callbacks are being called, which I am doing. 我见过的大多数响应都要求确保正在执行回调。 I'm also using React router if that makes a difference. 如果这有所作为,我也正在使用React路由器。 Any help would be greatly appreciated. 任何帮助将不胜感激。

import React, { Component } from 'react';
import { TweenMax } from 'gsap';
import PropTypes from 'prop-types';

import { Animated } from '../../components/common/Animated';
import NavButton from '../../components/navButton/NavButton';

import './Team.scss';

class Team extends Component {

  componentDidMount() {
    this.el = this.refs.container;
  }

  componentWillAppear(callback) {
    TweenMax.set(this.el, { x: '100%' });
    TweenMax.to(this.el, 1, { x: '0%' });
    callback();
  }

  componentWillLeave(callback) {
    TweenMax.to(this.el, 1, { x: '-100%', onComplete: callback });
  }

  render() {
    const { match } = this.props;
    return (
      <div ref="container" className="teamContainer">
        <NavButton
          title={'To Interactive'}
          route={`/${match.params.monster}/interactive`}
        />
      </div>
    );
  }
}

Team.propTypes = {
  match: PropTypes.shape({
    pathname: PropTypes.shape({
      monster: PropTypes.number.isRequired,
    }),
  }),
};

Team.defaultProps = {
  match: {
    pathname: {
      monster: -1,
    },
  },
};

export default Animated(Team);

I haven't tried this with react router, but with a similar router approach. 我还没有尝试过使用React Router,但是使用了类似的Router方法。 I got it to work using TransitionGroup v1.x and made sure my active child component was 我使用TransitionGroup v1.x使其工作,并确保我的活动子组件是

  1. a react element 反应元素
  2. had it's own unique key 有自己独特的钥匙

https://github.com/reactjs/react-transition-group/tree/v1-stable https://github.com/reactjs/react-transition-group/tree/v1-stable

Note: 注意:

When using CSSTransitionGroup, there's no way for your components to be notified when a transition has ended or to perform any more complex logic around animation. 使用CSSTransitionGroup时,无法在过渡结束时通知您的组件,也无法对动画执行任何更复杂的逻辑。 If you want more fine-grained control, you can use the lower-level TransitionGroup API which provides the hooks you need to do custom transitions. 如果需要更细粒度的控件,则可以使用较低级别的TransitionGroup API,该API提供进行自定义过渡所需的钩子。

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

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