Context:
I want to add an AOS-effect to multiple divs filled with data from a server, which are rendered after the initial render:
import {useEffect, useState} from "react";
export const Test = () => {
const [data, setData] = useState([]);
async function fetchData() {
//Example fetch request
const response = await fetch(API, {method:"GET"});
const json = await response.json();
setData(json);
}
useEffect(() => {fetchData();}, []);
return (
<>
{
data.map((text, index) => {
//add aos effect to each div
<div key={index}>
{text}
</div>
});
}
</>
)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
What I've tried:
Question:
How can I get animate on scroll (AOS) to work on fetched data in react?
CodePudding user response:
I found the error. I had to import the aos css files as well:
import AOS from "aos";
import "aos/dist/aos.css";
CodePudding user response:
Did you initialize AOS in the first place?
<script>
AOS.init();
</script>
You might need to refresh AOS due to reacts rendering nature, which renders new html elements on each component update.
AOS.refresh();
CodePudding user response:
I would try giving a class which animates or using Intersection Observer