加载组件时,不会调用TransitionGroup生命周期方法

时间:2017-12-07 16:11:35

标签: reactjs gsap react-transition-group

我正在尝试使用gsap设置进入和退出的列表的动画,因此我将列表包含在<TransitionGroup>中。我想使用componentWillEnter和componentWillLeave来触发我的动画,但它们没有被调用。我已经检查了所有1000x并且无法弄清楚...我应该使用<Transition>代替这种动画吗?

import React from "react";
import { TransitionGroup } from "react-transition-group";

import animations from './animations';

class Events extends React.Component {
  componentWillEnter(cb) {
    console.log('entered');
    const items = document.getElementsByClassName("items");
    animations.animateIn(items, cb);
  }
  componentWillLeave(cb) {
    console.log('exited');
    const items = document.getElementsByClassName("items");
    animations.animateOut(items, cb);
  }


  render() {
    const event = this.props.event;
    return (
      <li key={event._id} className="items">
        <h1>{event.title}</h1>
      </li>
    );
  }
}

class Main extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      events: []
    };
  }
  componentDidMount() {
    return fetch("https://pickup-btown.herokuapp.com/api/event/biking", 
    {
      method: "GET",
      headers: {
        "Content-Type": "application/json"
      },
      mode: "cors"
    })
      .then(response => {
        return response.json();
      })
      .then(events => {
        this.setState({ events: events.docs });
      })
      .catch(err => console.log(err));
  }

  unLoad(e) {
    e.preventDefault();
    this.setState({ events: [] });
  }
  render() {
    const events = this.state.events;
    return (
      <section>
        <button onClick={this.unLoad.bind(this)}>back</button>
        <TransitionGroup component="ul">
          {events.length ? (
            events.map(event => {
              return <Events event={event} key={event._id} />;
            })
          ) : (
            <div />
          )}
        </TransitionGroup>
      </section>
    );
  }
}

export default Main;

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

目前self.optimizer = tf.train.GradientDescentOptimizer(self.lr).minimize(self.loss) gvs = self.optimizer.compute_gradients(self.loss) 已删除子组件生命周期方法,您可以使用react-transition-grouponEnter方法实现onExit组件中的

Events

我已经处理过你的codeandbox及其工作。有关详细信息,请浏览documentation