Next.js Discord

Discord Forum

Infinite Scroll With Suspense

Unanswered
Little yellow ant posted this in #help-forum
Open in Discord
Little yellow antOP
How can i suspense only new items fetched?
Trouble: When i click in button to load more products, all data is reloading with suspense boundary, how can i structure to only fetch and suspense a new data?

EDIT: i need only new data in suspense

7 Replies

Little yellow antOP
Little yellow antOP
fetchData:
export async function fetchProperties(query: string, currentPage: number) {
  noStore()
  const totalPropertiesToFetch = currentPage * PROPERTY_PER_PAGE;

  try {
    const properties = await sql`
      SELECT
        properties.*
      FROM properties
      JOIN categories ON properties.category_id = categories.id
      JOIN locations ON properties.location_id = locations.id
      WHERE 
        properties.price::text ILIKE ${`%${query}%`} OR
        locations.city ILIKE ${`%${query}%`} 
      ORDER BY properties.created_at DESC
      LIMIT ${totalPropertiesToFetch}
    `;

    return properties.rows;    
  } catch (error) {
    console.error('Database Error:', error);
    throw new Error('Failed to fetch properties.');
  }
}
PropertiesList:
export default async function PropertiesList({ query, currentPage }: PropertiesListTypes) {
  const properties = await fetchProperties(query, currentPage);

  return (
    <div className="grid grid-cols-1 gap-5 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 2xl:grid-cols-5 3xl:grid-cols-6 4xl:grid-cols-7 mb-6">
      {properties.map((property) => (
        <PropertyCard key={property.id} data={property} />
      ))}
    </div>
  )
}
Pagination:
'use client';

import { usePathname, useSearchParams } from 'next/navigation';
import Link from 'next/link';

export default function Pagination({ totalPages }: { totalPages: number }) {
  const pathname = usePathname();
  const searchParams = useSearchParams();
  const currentPage = Number(searchParams.get('page')) || 1;

  const createPageURL = (pageNumber: number | string) => {
    const params = new URLSearchParams(searchParams);
    params.set('page', pageNumber.toString());
    return `${pathname}?${params.toString()}`;
  };

  return (
    <div className="inline-flex">
      <div className="flex mx-auto">
        {currentPage < totalPages && (
           <Link
            scroll={false}
            href={createPageURL(currentPage + 1)}
            className='px-6 py-2 border rounded text-emerald-50 font-medium bg-emerald-500'
          >
            Carregar mais
          </Link>
        )}
      </div>
    </div>
  );
}
Page:
// ...
<Suspense key={query + currentPage} fallback={<PropertyCardSkeletonList />}>
  <PropertiesList query={query} currentPage={currentPage} />
</Suspense>
<Pagination totalPages={totalPages} />
// ...
Hygen Hound
@Little yellow ant hey man, I'm also struggling with the same problem, have you found a solution to this?
Little yellow antOP
nope, i believe is not possible to doing that without a client component