I m trying to fetches movies OMDB API data you can see 2 fetches, each fetch give array of 10 objects data only,
but the data is being incremented on every search, even though I kept i.e reseting state setmovieData([])
at the beginning of the function.
useEffect(
function () {
async function requestMovieData() {
setmovieData([]);
const res1 = await fetch(
`http://www.omdbapi.com/?apikey=51b8382f&s=${query}`,
);
const res2 = await fetch(
`http://www.omdbapi.com/?apikey=51b8382f&s=${query}&page=1`,
);
const data1 = await res1.json();
const data2 = await res2.json();
if ((data1.Response = "True" && data1.Search)) {
setmovieData([...movieData, ...data1.Search]);
}
if ((data2.Response = "True" && data2.Search)) {
setmovieData([...movieData, ...data2.Search]);
}
}
requestMovieData();
},
[query],
);