To achieve the behavior you're looking for, you will need to make some modifications to your Next.js page component. You want the first page to load normally, then show a "View More Items" button, and upon clicking that button, continue to load more items using your InfScroll component as before.
Here's an outline of how you can manage this:
-
Use state to control whether the "View More Items" button is shown and when to initialize the infinite scroll.
-
Render the initial items on the page.
-
Only render the infinite scroll functionality after the "View More Items" button is clicked.
Here's a sample implementation:
import { useState } from 'react';
import InfScroll from './components/InfScroll';
import Loading from './components/Loading';
import EditButton from './components/EditButton';
const HomePage = () => {
const [loading, setLoading] = useState(false);
const [showInfScroll, setShowInfScroll] = useState(false);
// Mock function to simulate fetching initial items
const fetchInitialItems = () => {
// Replace with your fetch logic
return ['Item 1', 'Item 2', 'Item 3'];
};
const initialItems = fetchInitialItems();
return (
<div>
<EditButton url={"/admin/publishers/homepage"} />
{/* Render the initial items here */}
<div>
{initialItems.map((item, index) => (
<div key={index}>{item}</div>
))}
</div>
{/* Show the "View More Items" button initially */}
{!showInfScroll && (
<button onClick={() => setShowInfScroll(true)}>
View More Items
</button>
)}
{/* Render infinite scroll and loading components conditionally */}
{showInfScroll && <InfScroll onLoad={getMatters} loading={loading} />}
{loading && <Loading />}
</div>
);
};
export default HomePage;
Explanation:
-
Initial State: The useState hook is used to manage the showInfScroll state, which determines whether to display the InfScroll component.
-
Render Initial Items: The fetchInitialItems function is a mock to simulate fetching initial items. Replace this with your actual item-fetching logic.
-
Button for More Items: Initially, a "View More Items" button is shown. When it's clicked, showInfScroll is set to true, which triggers the rendering of the InfScroll component.
-
Conditional Rendering: The InfScroll component is rendered only after the "View More Items" button is clicked. The loading indicator is displayed based on your existing logic.
By following this approach, you can handle the initial page load separately from the infinite scrolling behavior.