NextJS documentation said, we can use server action to fetch data from client component.
Well in my case, it just says async/await is not yet supported in Client Components, only Server Components. This error is often caused by accidentally adding 'use client' to a module that was originally written for the server.
I don't know what's wrong here, I've checked multiple tutorials, they all say this is how it should be done, but it seems doesn't work at all anymore for me.
I'm trying to implement infinite scroll, and seems this is preventing me from finishing it
// ListingsOfAds.jsx
"use client";
import { useState, useEffect, useRef, useCallback } from "react";
import { AdItem } from "./AdItem";
import { fetchAdsServer } from "./fetchAds";
const ListingsOfAds = () => {
const [ads, setAds] = useState([]);
const [page, setPage] = useState(1);
const [loading, setLoading] = useState(false);
const [hasMore, setHasMore] = useState(true);
const observer = useRef(null);
const fetchAds = async (pageNum) => {
if (loading || !hasMore) return;
setLoading(true);
try {
/* const response = await axios.post("http://xxx.xxx.xxx/ads/list", {
type: "all",
page: page,
limit: 36,
status: "Published",
category: 2,
});
*/
const newAds = await fetchAdsServer(pageNum);
setAds((prevAds) => [...prevAds, ...newAds]);
//don't fetch if there's no more data.
if (newAds.lenght === 0) {
setHasMore(false);
}
} catch (error) {
console.error("Error: ", error);
}
setLoading(false);
};
"use server";
const axios = require("axios");
export async function fetchAdsServer(pageNum) {
const response = await axios.post("http://xxx.xxx.xxx/ads/list", {
type: "all",
page: pageNum,
limit: 36,
status: "Published",
category: 2,
});
return response.data.paidAds;
}