I am working on a InfiniteScroll with React and TypeScript. When I am setting Posts state using setPosts and setting there a page using setPage I have an error Expected 1 arguments, but got 2. How can I declare in state that I am going to use two parameters and one of them is a setPage?
const [posts, setPosts] = useState([]);
const [page, setPage] = useState();
const [isLoading, setIsLoading] = useState(false);
const lastItemRef = useRef(null);
const observer = useRef<IntersectionObserver>();
useEffect(() => {
fetchPosts().then((res: any) => {
setPosts([...res.posts], setPage(res.page));
});
}, []);
CodePudding user response:
You need to pass a single object with two properties like below to setPosts
setPosts({ posts: [...res.posts], page: res.page});
Alternatively, you need to set Posts and Page separately.
setPosts([...res.posts]);
setPage(res.page);
CodePudding user response:
setPosts() can have only one arguments. if you want save both posts and page. your code should be looks like this
setPosts([...posts, res.posts])
setPage(res.page) or setPage([...page, res.page])
I am not sure why you are using page hook, but I think this will be better to manage posts.
setPosts([...posts, {post: res.posts, page: res.page}])
or
setPosts([...posts, res])
and use posts.posts
, and posts.page