Kalan's Blog

Software Engineer / Taiwanese / Life in Fukuoka

Current Theme light

react-transition-group has several important changes when upgrading from v1 to v4:

  1. The CSSTransitionGroup component has been removed after the upgrade.
  2. leave has become exit.
  3. transitionName has been changed to classNames.
  4. Properties like transitionEnterTimeout have become part of the timeout object.
  5. transition{Appear, Enter, Leave} have been changed to appear, enter, exit.

If the following methods are used in the component, it becomes a bit more troublesome as they need to be removed manually:

  • componentWillAppear
  • componentDidAppear
  • componentWillEnter
  • componentDidEnter
  • componentWillLeave
  • componentDidLeave

If there are no strange hacks, you can also consider using jscodeshift for migration. If the component has become too large to migrate easily, you can create a wrapper that wraps the original props:

/**
 * NOTE:
 * This is temporary wrapper component for Transition.js,
 * It provides the same API as `CSSTransitionGroup` in v1
 */
import * as React from 'react';
import { TransitionGroup, CSSTransition } from 'react-transition-group';

type Props = {
  component?: React.ReactNode;
  transitionName: string;
  transitionEnterTimeout?: number;
  transitionExitTimeout?: number;
  transitionEnter?: boolean;
  transitionExit?: boolean;
  children: React.ReactNode,
};

const CSSTransitionGroup = ({
  transitionEnterTimeout= 0,
  transitionExitTimeout= 0,
  transitionEnter= true,
  transitionExit= true,
  transitionName,
  children,
  component,
}: Props) => {
  const transitionChildrenProps = {
    classNames: transitionName,
    timeout: { enter: transitionEnterTimeout, exit: transitionExitTimeout }
  };

  const transitions = React.Children.map(children, (child: any) => (
    <CSSTransition key={`transition-${child.key}`} {...transitionChildrenProps}>{child}</CSSTransition>
  ));

  return (
    <TransitionGroup
			component={component}
			enter={transitionEnter}
			exit={transitionExit}
 		>
      {transitions}
    </TransitionGroup>
  );
};

export default CSSTransitionGroup

With this wrapper, the migration can be painless. However, for easier maintenance, it is still recommended to gradually remove the deprecated code.

Prev

class component to hooks

Next

Driving Technical Change

If you found this article helpful, please consider buy me a drink ☕️ It'll make my ordinary day shine✨

Buy me a coffee

作者

Kalan 頭像照片,在淡水拍攝,淺藍背景

愷開 | Kalan

Hi, I'm Kai. I'm Taiwanese and moved to Japan in 2019 for work. Currently settled in Fukuoka. In addition to being familiar with frontend development, I also have experience in IoT, app development, backend, and electronics. Recently, I started playing electric guitar! Feel free to contact me via email for consultations or collaborations or music! I hope to connect with more people through this blog.