const fakeFetch = (delay = 400) =>
new Promise((res) => setTimeout(res, delay));
const fetchItems = async () => {
try {
setParams((state) => state + 1);
console.log(11111, params); //1
await fakeFetch();
const res = await axios.get(baseUrl + params, config);
setPosts(posts.concat(res.data.posts));
console.log(res);
} catch (err) {
console.log(err);
}
};
useEffect(() => {
fetchItems();
}, []);
const [_, setRef] = useIntersect(async (entry, observer) => {
console.log("hi"); //hi
console.log(params); //1
observer.unobserve(entry.target);
await fetchItems();
observer.observe(entry.target);
}, {});
我正在尝试实现无限滚动功能。我正在尝试使用Intersection Observer来实现它,但是该控制台Hi可以很好地输出并且功能可以很好地执行。但是,状态不会增加。这是为什么?我该如何解决?