· 2 min read
migrate react-transition-group
# Frontend This article was auto-translated from Chinese. Some nuances may be lost in translation.
Upgrading from react-transition-group v1 to v4 involves several key changes:
- The
CSSTransitionGroupcomponent has been removed. leavehas been renamed toexit.transitionNamehas changed toclassNames.- Properties like
transitionEnterTimeouthave been consolidated into atimeoutobject. transition{Appear, Enter, Leave}have been simplified toappear,enter, andexit.
If your component uses any of the following lifecycle methods, it’s a bit more complicated as you’ll need to remove them manually:
componentWillAppearcomponentDidAppearcomponentWillEntercomponentDidEntercomponentWillLeavecomponentDidLeave
If there are no peculiar hacks involved, you might consider using jscodeshift for the migration. If your component has become too complex to migrate easily, you can create a wrapper that encapsulates the original props:
/**
* NOTE:
* This is a 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 approach allows for a smooth transition. However, for ease of maintenance, it’s still advisable to gradually phase out these legacy features.
Related Posts
- What to Pay Attention to When Using Images on the FrontendBased on an article by Jake Archibald, this post organizes how modern responsive images should be written: why `width`/`height` still matter, when to use CSS `aspect-ratio`, how to choose between AVIF and WebP, and how to use `picture`/`source`/`srcset` for switching images on mobile.
- CSS field-sizing — Auto-Resize Form Elements with One Line of CSSMaking a textarea auto-resize used to require JavaScript to watch scrollHeight. CSS field-sizing: content replaces all of that in one line, supporting textarea, input, and select.
- Make Your Hyperlink Underlines Look Better: text-underline-offsetBy default, underlines sit very close to the text, and some designers dislike this style. Personally, I don’t think it looks very good either.
- Why Web Design Shouldn’t Chase Pixel PerfectOnly pay attention to Pixel Perfect when it really matters; otherwise, it often leads to a lose-lose situation.