There are scenarios where you may want elements to move, fade, or animate as the user scrolls. For example, when creating a parallax effect. Implementing this in React requires efficiently tracking the scroll position and updating styles to ensure smooth and seamless animations.
When you scroll a page, the DOM changes, but React doesn’t automatically know about the scroll position. If you try to directly modify the style in a render function, the component won’t update correctly.
To solve this, you need a reactive, performant way to update the component’s style based on the scroll position.
import React, { useState, useEffect } from "react";
function ParallaxComponent() {
const [offset, setOffset] = useState(0);
useEffect(() => {
let ticking = false;
const handleScroll = () => {
if (!ticking) {
window.requestAnimationFrame(() => {
setOffset(Math.min(0, window.scrollY / 3 - 60));
ticking = false;
});
ticking = true;
}
};
window.addEventListener("scroll", handleScroll, { passive: true });
return () => {
window.removeEventListener("scroll", handleScroll);
};
}, []);
return (
<div
style={{
transform: `translateY(${offset}px)`,
transition: "transform 0.1s linear",
height: "300px",
background: "#90caf9",
display: "flex",
justifyContent: "center",
alignItems: "center",
fontSize: "24px",
fontWeight: "bold",
}}
>
Scroll Me
</div>
);
}
export default ParallaxComponent;
Work with our skilled React developers to accelerate your project and boost its performance.
Hire Reactjs Developers