I was expecting just 10 pictures on initial load, but ended up getting 20 because of the functional update of the state.
Result:
if in use the normal approach the app will show the expected 10,i want to add if statement to the state so i need to use the functional update
app code below
import React, { useState, useEffect } from "react";
import { FaSearch } from "react-icons/fa";
import Photo from "./Photo";
const clientID = `?
client_id=${import.meta.env.VITE_ACCESS_KEY}`;
const mainUrl = `https://api.unsplash.com/photos/`;
const searchUrl = `https://api.unsplash.com/search/photos/`;
function App() {
const [loading, setLoading] = useState(false);
const [photos, setPhotos] = useState([]);
const [page, setPage] = useState(1);
const [query, setQuery] = useState("");
const fetchImages = async () => {
setLoading(true);
let url;
// const urlPage = `&page=${page}`;
const urlPage = `&page=${page}`;
const urlQuery = `&query=${query}`;
url = `${mainUrl}${clientID}${urlPage}`;
try {
const response = await fetch(url);
const data = await response.json();
console.log(data);
setPhotos((oldPhoto) => {
return [...oldPhoto, ...data];
});
setLoading(false);
} catch (error) {
console.log(error);
setLoading(false);
}
};
useEffect(() => {
fetchImages();
}, [page]);
useEffect(() => {
const event = window.addEventListener("scroll", () => {
if (
!loading &&
window.innerHeight + window.scrollY >= document.body.scrollHeight - 2
) {
setPage((oldPage) => {
return oldPage + 1;
});
}
});
return () => window.removeEventListener("scroll", event);
}, []);
const handleSubmit = (e) => {
e.preventDefault();
};
return (
<main>
<section className="search">
<form className="search-form">
<input
type="text"
placeholder="search"
className="form-input"
value={query}
onChange={(e) => setQuery(e.target.value)}
/>
<button type="submit" className="submit-btn" onClick={handleSubmit}>
<FaSearch />
</button>
</form>
</section>
<section className="photos">
<div className="photos-center">
{photos.map((image) => {
return <Photo key={image.id} {...image} />;
})}
</div>
{loading && <h2 className="loading">loading...</h2>}
</section>
</main>
);
}
export default App;