Search code examples
reactjsgsapreact-transition-group

React TransitionGroup lifecycle methods not being called when component is loaded


I'm trying to animate a list entering and exiting with gsap, so I wrapped my list with <TransitionGroup>. I want to use componentWillEnter and componentWillLeave to trigger my animations, but they aren't being called. I've checked everything 1000x and can't figure it out... Should I be using <Transition> instead for this sort of animation?

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;

Any help would be much appreciated!


Solution

  • Child component life cycle methods has been removed in current react-transition-group and you can use onEnter and onExit methods to achieve like in your Events component will be

    class Events extends React.Component {
      render() {
        const event = this.props.event;
        return (
          <Transition
            timeout={300}
            key={event._id}
            onEntering={el => console.log("Entering", el)}
            onEnter={el => console.log("enter", el)}
            onExit={el => console.log("exit", el)}
            in={true}
          >
            <li
              key={event._id}
              className="items"
            >
              <h1>{event.title}</h1>
            </li>
          </Transition>
        );
      }
    }
    

    I have worked on your codesandbox also and its working. For detailed info please go through documentation.