I’m working on a React project where I have multiple components that render conditionally based on state changes. I want to implement a slide-down and fade-in effect for one of these components when it gets rendered. Below is a simplified version of my code:
import React, { useState, useRef, useEffect } from "react";
import { TypeAnimation } from "react-type-animation";
function Landing() {
const [renderNumber, setRenderNumber] = useState(1);
const typeAnimRef = useRef(null);
useEffect(() => {
if (renderNumber === 1) {
const timer = setTimeout(() => {
setRenderNumber(2);
}, 4000);
return () => clearTimeout(timer);
}
if (renderNumber === 2) {
const timer = setTimeout(() => {
setRenderNumber(3);
}, 4000);
return () => clearTimeout(timer);
}
}, [renderNumber]);
return (
<div className="w-full py-5">
<div className="mx-auto relative flex flex-row">
<div className="flex flex-col gap-3 font-black">
<div>
<TypeAnimation
ref={typeAnimRef}
sequence={[
"Divy",
1000,
"Divy Prakash",
1000,
"Divy Prakash Pandey",
1000,
]}
speed={50}
style={{ fontSize: "2em" }}
wrapper="div"
deletionSpeed={Infinity}
/>
</div>
<div>
{renderNumber >= 2 && (
<div
className={`transition-opacity transform ease-in-out duration-1000 delay-1000 ${
renderNumber >= 2
? "opacity-100 translate-y-0"
: "opacity-0 -translate-y-4"
}`}
>
<TypeAnimation
sequence={[
"FULL",
1000,
"FULL STACK",
1000,
"FULL STACK WEB DEVELOPER",
1000,
]}
speed={50}
style={{ fontSize: "2em" }}
wrapper="div"
/>
</div>
)}
</div>
{renderNumber === 3 && (
<div>
<TypeAnimation
style={{
whiteSpace: "pre-line",
height: "195px",
display: "block",
}}
sequence={[
`I am anFull Stackn Web DevelopernFrom India`,
1000,
]}
repeat={Infinity}
/>
</div>
)}
</div>
</div>
</div>
);
}
export default Landing;
export default Landing;
In this code, I attempted to implement a slide-down and fade-in animation effect for the second TypeAnimation
component as soon as it gets rendered, using Tailwind CSS transition classes. However, I’m not sure if this is the correct approach or if there’s a better way to achieve the desired effect.
How can I achieve a smooth slide-down and fade-in effect for the component when it renders? Is there a better approach to achieve this animation in React with Tailwind CSS?