Intersection observer only works with the last element in Next.js
I want to target all the repeated div elements and put an opacity effect(0 or 1) every time I see them on the viewport, but only the last div element ends up having the opacity effect.
This is my code:
const AboutMe: NextPage = ({ data }: any) => {
const target = useRef<HTMLDivElement>(null);
useEffect(() => {
let observer: IntersectionObserver;
if (!target.current) return;
if (target) {
observer = new IntersectionObserver(
([e]) => {
const target = e.target as HTMLElement;
if (e.isIntersecting) {
target.style.opacity = "1";
} else {
target.style.opacity = "0";
}
},
{ threshold: 0.5 }
);
observer.observe(target.current as Element);
}
}, [target]);
return (
<main>
<section className="container px-5 py-24 mx-auto space-y-20">
{data.results?.map((item: any) => {
return (
<div
className="opacity-0 transition-all duration-500"
key={item.id}
ref={target}
>
<ol>
<li>
{
item.properties.content.rich_text[0]
.plain_text
}
</li>
</ol>
</div>
);
})}
</section>
</main>
);
};
export default AboutMe;
export async function getStaticProps() {
const options = {
...
};
const res = await fetch(
`https://api...`,
options
);
const data = await res.json();
return {
props: { data },
};
}
How can I solve this?