I work on a basic infinite scroll and I've got a problem getting the data from Items
.
It works in my useEffect
call, I console.log
it, and everything is there.
But then the value set to 1... why?
Thanks
const List = () => {
const [Items, setItems] = useState([]);
const [fetchData, setFetchData] = useState((listSize > lot) ? lot : listSize);
const [Limits, setLimits] = useState(true);
console.log(Items); // got empty first time and then 1
useEffect(() => {
let nvxPic = list.slice(fetchData - lot, fetchData);
setItems(Items.push(nvxPic.map((image, key) => (
<img key={key} src={`Images/${image}`} alt="" />
))))
console.log(Items); // got my array
}, [fetchData])
const fetchMoreData = async () => {
if (fetchData + lot > listSize) {
setFetchData(listSize);
setLimits(false);
}
else
setFetchData(fetchData + lot);
};
return (
<div>
<InfiniteScroll
dataLength={fetchData}
next={fetchMoreData}
hasMore={Limits}
loader={<h4>Loading...</h4>}
endMessage={
<p style={{ textAlign: "center" }}>
<b>Done</b>
</p>
}
>
{Items}
</InfiniteScroll>
</div>
);
};