· 2 min read
Migrating react-transition-group
This article was auto-translated from Chinese. Some nuances may be lost in translation.
Here are several key points to note when upgrading react-transition-group from v1 to v4:
- After the upgrade, the
CSSTransitionGroupcomponent was removed. leavebecameexit.transitionNamebecameclassNames.- Props like
transitionEnterTimeoutwere replaced by atimeoutobject. transition{Appear, Enter, Leave}becameappear,enter,exit.
If your components use any of the following lifecycle methods, it will be a bit more troublesome, as you’ll have to remove them yourself:
componentWillAppearcomponentDidAppearcomponentWillEntercomponentDidEntercomponentWillLeavecomponentDidLeave
If you don’t have any strange hacks, you can also consider using jscodeshift for the migration. If your components have grown too bloated to migrate easily, you can create a wrapper component to adapt the legacy 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
This way, you can achieve a painless migration. However, for the sake of maintainability, it is still better to phase it out gradually.
Related Posts
- Recreating My Room with Three.js Using React Three Fiber, I brought my real room into the browser—turning physical objects into an interactive table of contents, and using spatial memory to tell the story of my life and work over the past few years.
- Things to Keep in Mind When Using Images in Frontend Development Expanding on Jake Archibald's article, this post organizes how modern responsive images should be written: why width/height are still necessary, when to use CSS aspect-ratio, how to choose between AVIF and WebP, and using picture/source/srcset for art direction on mobile devices.
- CSS field-sizing — Auto-resize Form Elements with a Single Line of CSS Previously, auto-resizing a textarea required listening to scrollHeight in JavaScript. With CSS field-sizing: content, a single line replaces it all, supporting textarea, input, and select. This article covers the pain points of older approaches and how to use field-sizing.
- Make Your Link Underlines Look Better: text-underline-offset By default, underlines sit very close to the text. Some designers dislike this look, and personally, I don't think it looks great either.