AG Grid React Tutorial: Build High-Performance Data Tables
Quick summary: This concise, hands-on guide gets you from installation to common features—sorting, filtering, pagination, and cell editing—plus performance tips and a working example using AG Grid with React.
Why choose AG Grid for React data tables?
When you need a production-ready grid that can handle thousands of rows, complex column types, and enterprise features, AG Grid is a go-to. It’s optimized for performance, provides a rich API, and supports both community (free) and enterprise features. For React developers, AG Grid offers a native wrapper—AgGridReact—that fits the component model and lifecycle.
Unlike lightweight table libraries that focus on simple use cases, AG Grid is built to scale: virtual DOM rendering, row virtualization, and selective re-rendering all reduce work when data changes. That means smoother scrolling, snappier sorting, and reliable behavior when interacting with large datasets.
Finally, AG Grid’s ecosystem includes cell editors, filters, aggregation, clipboard support, and export utilities—so you won’t waste time wiring these from scratch. If you’re starting out, the official AG Grid React docs and community tutorials are excellent resources; here’s a practical starter guide I recommend: AG Grid React tutorial.
Installation & initial setup (fast path)
Setups vary by bundler, but with create-react-app or Vite the pattern is identical: install the packages, import the CSS theme, and render AgGridReact as a component. The community package covers most features you’ll use for standard applications.
Run one of the following in your project root to add AG Grid to a React app:
npm install ag-grid-community ag-grid-reactoryarn add ag-grid-community ag-grid-react
Then import styles and the component in your React file. For example:
import 'ag-grid-community/styles/ag-grid.css';
import 'ag-grid-community/styles/ag-theme-alpine.css';
import { AgGridReact } from 'ag-grid-react';
If you need enterprise features, also install ag-grid-enterprise and initialize the license per the docs. For detailed installation instructions and common pitfalls, AG Grid’s official docs are indispensable: AG Grid React docs.
Core concepts: columns, rows, and gridOptions
At the center of AG Grid are column definitions (columnDefs) and row data (rowData). columnDefs define behavior and presentation for each column—header name, field, formatter, editors, and whether the column is sortable or filterable. rowData is simply an array of objects; AG Grid maps those objects to columns by the field name.
gridOptions centralizes configuration and exposes the grid API. You can pass gridOptions or props to the AgGridReact component—common options include pagination, defaultColDef, and frameworkComponents. For straightforward apps, defaultColDef lets you apply defaults like resizable and sortable to all columns, reducing repetition.
The grid API (available via refs) exposes programmatic control: refreshView(), setRowData(), getSelectedRows(), and pagination API methods among many others. Event callbacks—onGridReady, onCellValueChanged, onFilterChanged—help you react to user actions and synchronize grid state with React state or external stores.
Common features implemented (sorting, filtering, pagination, cell editing)
Most data-table needs fall into these categories: sorting, filtering, pagination, and editing. In AG Grid, enable these per-column or globally. For sorting and filtering, set sortable: true and filter: true on columnDefs or rely on defaultColDef for uniform behavior. Filters include text, number, set, and custom filters for complex scenarios.
Pagination is a grid-level feature: set pagination: true and configure paginationPageSize. AG Grid also supports infinite scrolling and server-side row model for very large data sets—use server-side row model if you want the grid to fetch only visible data pages from the backend.
Cell editing is straightforward: mark a column editable: true to enable inline editing or supply a custom cellEditor component for complex editors. Capture edits with the onCellValueChanged event to validate or persist changes back to your server. The grid supports transactions for efficient updates (transaction API) so you can batch inserts/updates/deletes without re-rendering everything.
Performance & best practices for React
Performance is a key reason teams pick AG Grid. To keep your React app responsive, prefer the grid’s row virtualization and avoid passing new object references to columnDefs and gridOptions on every render. Memoize columns and options with useMemo and keep row data updates minimal—use transaction updates if you change only a few rows.
Leverage the right row model: the client-side row model is great for moderate datasets (tens of thousands of rows). For millions of rows, use the server-side row model or infinite scrolling with server-side pagination to keep memory and DOM usage bounded. Also, prefer value getters and cell renderers that are pure and fast; expensive computations should be hoisted out or memoized.
Finally, integrate AG Grid with React state management carefully. Use controlled patterns when necessary, but avoid forcing the grid to rehydrate from React state on every tiny change. When you do update React state from grid events, debounce or batch setState calls and rely on the grid API where possible to perform quick UI updates.
Example walkthrough: a simple editable table
Below is a compact example showing a working AG Grid React component with sorting, filtering, pagination, and editable cells. This pattern uses functional components, hooks, and memoized configs for performance.
import React, { useMemo, useRef, useState } from 'react';
import { AgGridReact } from 'ag-grid-react';
function SimpleGrid() {
const gridRef = useRef();
const [rowData, setRowData] = useState([
{ id:1, name:'Alice', age:30 },
{ id:2, name:'Bob', age:24 }
]);
const columnDefs = useMemo(()=>[
{ headerName:'ID', field:'id', sortable:true, filter:true, width:90 },
{ headerName:'Name', field:'name', editable:true, sortable:true, filter:true },
{ headerName:'Age', field:'age', editable:true, sortable:true, filter:'agNumberColumnFilter' }
], []);
return (
<div className="ag-theme-alpine" style={{height:400}}>
<AgGridReact
ref={gridRef}
rowData={rowData}
columnDefs={columnDefs}
pagination={true}
paginationPageSize={10}
onCellValueChanged={e => console.log('edited', e)}
/>
</div>
);
}
This snippet demonstrates the essentials: memoized columnDefs, rowData state, and event handling. Replace console logging with API calls to persist changes. To add custom editors or formatters, pass frameworkComponents and implement them as React components.
For a step-by-step beginner tutorial with a similar example, check out this practical guide: Getting started with AG Grid in React. It walks through creating your first data table and explains the key props used above.
Integration tips: forms, spreadsheets, and third-party libs
If you’re integrating AG Grid into forms or spreadsheet-like apps, treat the grid as the editable surface and synchronize important changes to your form state only when necessary—on blur, on submit, or via explicit save actions. That keeps the UI snappy and reduces state churn in React.
AG Grid can also serve as a lightweight spreadsheet by combining editable cells, custom cell editors, and clipboard API. For heavy spreadsheet features (formulas, Excel-like behavior), consider specialized libraries—but for many use-cases AG Grid’s editing and clipboard features are sufficient.
When combining AG Grid with other libraries (Redux, React Query, or remote data sources), choose patterns that minimize re-renders: use immutable updates for rowData, fetch paged data for large datasets, and use the grid API to apply transactions so React rendering stays efficient.
Common pitfalls and quick fixes
One frequent issue is forgetting to memoize columnDefs and defaultColDef, which can cause the grid to re-render excessively. Wrap these objects in useMemo to return stable references across renders. Another pitfall is incorrect CSS imports—without loading the theme CSS the grid will not render properly.
If your data seems missing or columns behave unexpectedly, verify that the field names in columnDefs match keys in rowData. For pagination or server-side setups, ensure your server returns the expected metadata (total rows, page size) and that you use the correct row model.
Finally, if you see sluggish editing or slow initial renders with large data sets, switch to a row model that fetches only visible data (infinite or server-side), enable row virtualization, and reduce the number of complex cell renderers rendered simultaneously.
Recommended props and APIs to learn first
Start with these essentials: columnDefs, rowData, defaultColDef, onGridReady, and the grid API via refs. They cover most basic interactions and provide a foundation for advanced features. Learn how to use onCellValueChanged and transaction updates to manage edits efficiently.
Below are a few properties and API entry points you’ll use constantly; they’re short to learn but unlock the grid’s power.
pagination,paginationPageSize,sortable,filter,editable
After mastering these, explore framework-specific integrations like React cell renderers and cell editors, then the server-side and infinite row models for scaling. Documentation and community examples are invaluable—bookmark the official docs for reference: AG Grid React docs.
FAQ
How do I install AG Grid in a React project?
Install the community packages via npm or yarn: npm install ag-grid-community ag-grid-react. Import the theme CSS (for example, ag-theme-alpine) and the AgGridReact component into your React component, then render it with columnDefs and rowData.
How do I enable sorting, filtering, and pagination?
Enable sorting and filtering on columns with sortable:true and filter:true. Add pagination by setting pagination:true on the grid and configure paginationPageSize. Use the grid API for programmatic control and more advanced behaviors.
Can I edit cells and capture changes?
Yes—set editable:true on columns or implement custom editors. Listen to onCellValueChanged or use transaction APIs to commit edits and synchronize with server-side data or application state.
Semantic core (keyword clusters)
Secondary keywords: React data table, AG Grid React example, interactive table React, AG Grid filtering sorting, React grid component, AG Grid pagination
Clarifying / LSI phrases: React spreadsheet table, AG Grid cell editing, React data grid library, ag-grid-community, ag-grid-react, AgGridReact, sortable columns React, filterable table React, virtualized grid React, server-side row model, paginationPageSize
Intent groups:
– Informational: „AG Grid tutorial”, „AG Grid React example”, „React data grid library”
– Navigational: „AG Grid React docs”, „ag-grid-react”
– Commercial: „ag-grid-enterprise license”, „enterprise grid React”
– Transactional: „install ag-grid react”, „ag-grid npm install”