Update TransitionAnimation.tsx

Refactor TransitionAnimation component to use refs and avoid deprecated findDOMNode

- Added useRef hook to reference the DOM node directly
- Passed nodeRef to the Transition component
- Simplified state management and useEffect hook
- Cleaned up props destructuring for clarity
- Set default empty string for className to simplify handling

This update ensures compatibility with future React releases and adheres to modern React best practices.
This commit is contained in:
Josh 2024-07-23 00:53:38 +01:00 committed by GitHub
parent eb865730bb
commit 48e74c81f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,9 +1,8 @@
import { FC, ReactNode, useEffect, useState } from 'react';
import { Transition } from 'react-transition-group';
import { getTransitionAnimationStyle } from './TransitionAnimationStyles';
import { FC, ReactNode, useEffect, useRef, useState } from "react";
import { Transition } from "react-transition-group";
import { getTransitionAnimationStyle } from "./TransitionAnimationStyles";
interface TransitionAnimationProps
{
interface TransitionAnimationProps {
type: string;
inProp: boolean;
timeout?: number;
@ -11,39 +10,40 @@ interface TransitionAnimationProps
children?: ReactNode;
}
export const TransitionAnimation: FC<TransitionAnimationProps> = props =>
{
const { type = null, inProp = false, timeout = 300, className = null, children = null } = props;
export const TransitionAnimation: FC<TransitionAnimationProps> = ({
type,
inProp,
timeout = 300,
className = "",
children,
}) => {
const nodeRef = useRef<HTMLDivElement>(null);
const [isChildrenVisible, setChildrenVisible] = useState(false);
useEffect(() =>
{
let timeoutData: ReturnType<typeof setTimeout> = null;
useEffect(() => {
let timeoutId: ReturnType<typeof setTimeout> | null = null;
if(inProp)
{
if (inProp) {
setChildrenVisible(true);
}
else
{
timeoutData = setTimeout(() =>
{
} else {
timeoutId = setTimeout(() => {
setChildrenVisible(false);
clearTimeout(timeout);
}, timeout);
}
return () =>
{
if(timeoutData) clearTimeout(timeoutData);
return () => {
if (timeoutId) clearTimeout(timeoutId);
};
}, [inProp, timeout]);
return (
<Transition in={ inProp } timeout={ timeout }>
{ state => (
<div className={ (className ?? '') + ' animate__animated' } style={ { ...getTransitionAnimationStyle(type, state, timeout) } }>
<Transition nodeRef={nodeRef} in={inProp} timeout={timeout}>
{(state) => (
<div
ref={nodeRef}
className={`${className} animate__animated`}
style={getTransitionAnimationStyle(type, state, timeout)}
>
{isChildrenVisible && children}
</div>
)}