Back to cookbook

AI Prompt to Build a React Infinite Scroll List With Virtualization

0 views Updated

Make this prompt yours

Share

This prompt is for developers who need a React infinite scroll list that stays fast even with thousands of rows, by combining lazy-loaded pagination with list virtualization so only the visible rows are ever rendered to the DOM. It's written for anyone using ChatGPT, Claude, or Gemini to scaffold a feed, search results page, or data-heavy list component without writing the scroll-detection and windowing logic from scratch.

A plain infinite scroll implementation that just appends more items to the DOM works fine for a few hundred rows, but it slows down badly once a list grows into the thousands, because the browser still has to lay out and paint every row that's ever been loaded. Virtualization fixes this by only mounting the rows currently in or near the viewport and swapping them out as the user scrolls, while a separate pagination layer fetches new pages of data before the user hits the bottom. The prompt asks the model to produce both pieces together: the data-fetching hook and the virtualized rendering layer, so they stay in sync instead of fighting each other.

Once you have a working version, it's worth running the generated code through a pass that trims unnecessary verbosity, especially if you're pasting large component files back into follow-up prompts. The Token Optimizer can compress a bulky prompt or long code block before you reuse it in later iterations.

Prompt template

Make this prompt yours

prompt-template
325 tokens
ROLE: You are a senior React developer who specializes in performant list rendering for data-heavy interfaces. CONTEXT: Framework: [REACT VERSION, e.g. React 18 with hooks] Data source: [DESCRIBE THE API OR DATA SOURCE, e.g. a paginated REST endpoint returning 50 items per page] List item shape: [DESCRIBE THE FIELDS EACH ROW NEEDS TO DISPLAY] Expected list size: [APPROXIMATE TOTAL ROWS, e.g. up to 10,000] TASK: Build a React component that renders this list with infinite scroll and virtualization: 1. A custom hook that manages paginated fetching, tracks loading and error state, and triggers the next page fetch when the user nears the bottom of the list. 2. A virtualized list renderer that only mounts rows near the current viewport, using a windowing technique, with a small buffer of extra rows above and below the visible area. 3. Loading placeholders for in-flight pages and a clear end-of-list indicator when there is no more data. 4. Basic error handling that lets the user retry a failed page fetch. CONSTRAINTS: - Use functional components and hooks only - Avoid unnecessary re-renders of rows that haven't changed - Assume variable row heights unless told otherwise - Include brief inline comments explaining the virtualization math OUTPUT FORMAT: 1. A short explanation of the approach 2. The custom data-fetching hook as a complete code block 3. The virtualized list component as a complete code block 4. Notes on any assumptions made about row height or page size

Want it sharper? Optimize this prompt with Prompt Optimizer, check it with the Prompt Debugger or shorten it with the Token Optimizer.

Example input

example-input
305 tokens
ROLE: You are a senior React developer who specializes in performant list rendering for data-heavy interfaces. CONTEXT: Framework: React 18 with hooks Data source: A paginated REST endpoint at /api/articles?page=N returning 50 articles per page as JSON List item shape: Each article has an id, title, author, and a short summary of variable length Expected list size: Up to 20,000 articles TASK: Build a React component that renders this list with infinite scroll and virtualization: 1. A custom hook that manages paginated fetching, tracks loading and error state, and triggers the next page fetch when the user nears the bottom of the list. 2. A virtualized list renderer that only mounts rows near the current viewport, using a windowing technique, with a small buffer of extra rows above and below the visible area. 3. Loading placeholders for in-flight pages and a clear end-of-list indicator when there is no more data. 4. Basic error handling that lets the user retry a failed page fetch. CONSTRAINTS: - Use functional components and hooks only - Avoid unnecessary re-renders of rows that haven't changed - Assume variable row heights unless told otherwise - Include brief inline comments explaining the virtualization math OUTPUT FORMAT: 1. A short explanation of the approach 2. The custom data-fetching hook as a complete code block 3. The virtualized list component as a complete code block 4. Notes on any assumptions made about row height or page size

When to use it

  • You're building a feed, search results list, or table that could grow into the thousands of rows
  • Users on lower-end devices are reporting lag or jank when scrolling through a long list
  • You're already fetching paginated data from an API and need the rendering layer to keep up without loading everything at once
  • You want to avoid hand-rolling scroll-position math and intersection observers from scratch

Best practices

  • Separate the data-fetching logic (which page is next, loading state, error state) from the rendering logic (which rows are currently visible) so each can be tested and reused independently
  • Use a windowing approach that renders a small buffer of off-screen rows above and below the viewport, rather than rendering only exactly what's visible, to avoid blank flashes during fast scrolling
  • Specify an approximate or measured row height up front, since most virtualization approaches need this to calculate scroll position accurately
  • Ask for loading skeletons or placeholders for in-flight pages instead of a blank gap, so the list doesn't visibly jump as new data arrives

Common mistakes

  • Appending every fetched page directly to the DOM without virtualization, which works at first but degrades badly as the list grows
  • Triggering the next page fetch only when the user reaches the very last pixel of the list, instead of a bit before it, causing a visible loading stall
  • Using a fixed row height in the virtualization math when rows actually vary in height, which causes jumpy scrolling and misaligned items
  • Forgetting to handle the empty state and the end-of-list state, leaving the component stuck showing a loading spinner forever

FAQs

What is list virtualization in React?

List virtualization is a rendering technique where only the rows currently visible in (or near) the viewport are mounted to the DOM, while the rest stay unrendered until the user scrolls to them, keeping long lists fast.

How is infinite scroll different from virtualization?

Infinite scroll is about fetching more data as the user nears the bottom of a list. Virtualization is about rendering performance once that data is loaded. A large list usually needs both working together, since infinite scroll alone can still slow the page down once thousands of rows are in the DOM.

Do I need a library for this, or can I build it from scratch?

You can build a basic version from scratch for learning or simple cases, but for production apps with large or variable-height lists, a dedicated virtualization library is usually more reliable than hand-rolled windowing math.

Which Cuelara tool helps me clean up this generated code?

Token Optimizer — compresses a long prompt or bulky generated component before you paste it into a follow-up prompt, cutting unnecessary tokens without losing the logic.

Found this prompt useful? Share it.

Share

More in React Prompt Recipes

React Prompt Recipes

AI Prompt to Turn a Design Screenshot into a React Component

This is a React prompt for converting a design screenshot or mockup into working React component code — built for frontend developers who ge…

ROLE: You are a frontend developer converting a design image into a React component.

CONTEXT: I'm attaching a design screenshot/mockup. Tech stack: [REACT VERSION, STYLING APPROACH e.g. Tailwind/CSS Modules/styled-components, COMPONENT LIBRARY IF ANY]

TASK:
1. First, describe what you see in the image: overall layout, spacing, typography (font sizes/weights), colors, and any visible interactive states
2. Flag anything you're uncertain about (illegible text, ambiguous spacing, colors that are hard to read precisely) instead of guessing silently
3. Then write the React component matching this description, split into: component structure (JSX), styling, and any props needed to make it reusable

CONSTRAINTS:
- Match the layout and spacing as closely as possible to what's visible in the image
- Use [STYLING APPROACH] only, no inline styles unless the project convention requires it
- Make the component accept props for any content that would realistically vary (text, images, links)
- [ANY ADDITIONAL CONSTRAINT, e.g. must be accessible, must support dark mode]

OUTPUT FORMAT:
1. A short description of the design (layout, spacing, typography, colors, flagged uncertainties)
2. The complete component code in a fenced code block
3. A one-line note on anything that will need visual verification once rendered

Make this prompt yours

React Prompt Recipes

ChatGPT Prompt to Build a React Drag-and-Drop File Upload Component With Progress Bar

This React drag and drop file upload component prompt is built for developers who need a working uploader with a drop zone, file previews, a…

ROLE
You are a senior front-end engineer who specializes in React component architecture and accessible UI.

CONTEXT
I'm building a [FORM / DASHBOARD / ADMIN PANEL] in React using [REACT VERSION, e.g. React 18 with function components] and [STYLING APPROACH, e.g. Tailwind CSS / CSS Modules / styled-components]. I need a file upload component that supports drag-and-drop in addition to a standard click-to-browse input.

REQUIREMENTS
- Accept these file types: [ALLOWED FILE TYPES, e.g. .png, .jpg, .pdf]
- Enforce a max file size of [MAX FILE SIZE] and a max file count of [MAX FILE COUNT]
- Show a visually distinct drop zone state when a file is dragged over it
- Display a progress bar per file while it uploads, using [UPLOAD METHOD, e.g. an onUploadProgress callback from axios, or XMLHttpRequest progress events]
- Show clear success and error states per file, including a retry option on failure
- Accept the actual upload function as a prop so the component is backend-agnostic
- Be keyboard accessible: the file input must be triggerable without a mouse, and status changes must be announced via an ARIA live region

CONSTRAINTS
- Use only [ALLOWED LIBRARIES, e.g. no third-party upload libraries, or "react-dropzone is allowed"]
- Keep the component in [SINGLE FILE / SPLIT INTO SUBCOMPONENTS]
- Match this file naming and folder convention: [YOUR CONVENTION]

OUTPUT FORMAT
1. The complete component code in a single fenced code block, with inline comments explaining the drag event handlers and progress logic
2. A short list of the props the component expects and what each one does
3. One sentence noting any edge case the implementation does not handle, if any

Make this prompt yours

React Prompt Recipes

AI Prompt to Build a React Multi-Step Form Wizard With Validation

This is an AI prompt for building a React multi step form wizard : a form split across several steps with a progress indicator, per step val…

ROLE: You are a senior frontend engineer who specializes in React form architecture.

CONTEXT:
- Framework: [REACT VERSION, e.g. React 18 with hooks]
- Styling approach: [CSS MODULES / TAILWIND / STYLED-COMPONENTS]
- Form library preference: [NONE - PLAIN STATE / REACT HOOK FORM / FORMIK]
- Steps needed: [LIST EACH STEP AND ITS FIELDS, e.g. Step 1: Account (email, password). Step 2: Profile (name, company). Step 3: Preferences (plan, notifications)]
- Validation rules: [LIST RULES PER FIELD, e.g. email must be valid format, password min 8 characters]

TASK:
Build a multi-step form wizard component with the following requirements:
1. A shared state object holding data from all steps, so navigating back and forth never loses entered values
2. Per-step validation that blocks the "Next" button until the current step's required fields are valid, with inline error messages
3. A progress indicator showing the current step out of the total number of steps
4. Back and Next navigation, with Back always enabled (except on step 1) and Next disabled until validation passes
5. A final review step before submission showing a summary of all entered data, with the ability to jump back to any step to edit it
6. A loading/submitting state on final submit that disables the submit button to prevent double submission
7. Clear separation between step configuration (data) and the rendering logic, so adding or removing a step does not require rewriting the component

CONSTRAINTS:
- [SPECIFY ACCESSIBILITY REQUIREMENTS, e.g. keyboard navigable, ARIA labels on progress indicator]
- [SPECIFY WHETHER STATE SHOULD PERSIST ACROSS PAGE REFRESH, e.g. using localStorage or session state]
- [SPECIFY IF STEPS ARE CONDITIONAL, e.g. step 3 only appears if plan = enterprise]

OUTPUT FORMAT:
Provide the complete component code, broken into logical files or sections (step config, main wizard component, individual step components, validation logic). Add brief comments explaining the state flow between steps. After the code, list any assumptions made about fields or behavior that were not specified above.

Make this prompt yours