DEV Community

Cover image for How to Build a Reusable React Grid Component with TypeScript
Vishal Porwal
Vishal Porwal

Posted on Edited on

How to Build a Reusable React Grid Component with TypeScript

React data grids are often among the most frequently reused components in enterprise React applications. They appear on customer dashboards, order management screens, financial reports, inventory pages, and administrative tools.
However, reusing a grid JavaScript Libraries does not automatically mean that grid implementations are consistent.
Different teams may configure date formats differently, implement editing and validation separately, or handle API errors differently. Over time, this creates duplicated code and inconsistent user experiences.
A reusable React grid component solves this by separating what a screen displays from how the grid behaves.

  1. Start with a small component API
    The first step is deciding what the shared component should expose.
    Avoid passing every underlying grid-library option directly to every screen. That creates a wrapper without establishing meaningful standards.
    Instead, define a focused API:
    columns: field, header, type, width, and editing options
    data: local records
    url: remote data endpoint
    groupBy: grouping field
    title: grid title
    onSelectionChange: selected rows
    onCellEdit: edit and save handling
    ref: methods such as reload() and clearFilters()
    This gives developers a predictable interface without requiring them to understand the underlying grid configuration.

  2. Standardize column definitions
    Column definitions are a good place to centralize formatting and editing rules.
    For example, define supported column types:
    Type
    Display
    Editor
    Text
    Plain text
    Text field
    Number
    Formatted number
    Number field
    Currency
    Currency format
    Number field
    Date
    Formatted date
    Date picker
    Boolean
    Yes/No
    Checkbox

A column mapper can translate these simple definitions into the configuration expected by the grid library.
This keeps formatting decisions in one place. If the application changes its currency or date format, the team updates the mapper rather than modifying every screen.

  1. Separate grid configuration from data updates
    One important performance consideration is avoiding unnecessary grid reconstruction.
    Structural configuration—such as columns, grouping, and data source—can be memoized using React's useMemo.
    When local records change, update the existing store rather than rebuilding the entire grid.
    This approach can help preserve useful grid state, including scroll position, sorting, and column widths.
    It is also important to keep column definitions stable. Defining column arrays outside the component is one practical way to avoid creating new references on every render.

  2. Make editing and validation consistent
    Inline editing should follow the same rules across the application.
    A shared component can expose an onCellEdit callback containing:
    The affected row
    The field being edited
    The new value
    The previous value
    The parent screen handles the API request, while the grid component controls whether the edit is committed or rolled back.
    For example:
    async function saveOrder(
    id: unknown,
    patch: Record
    ) {
    const response = await fetch(/api/orders/${id}, {
    method: "PATCH",
    headers: {
    "Content-Type": "application/json",
    },
    body: JSON.stringify(patch),
    });

if (!response.ok) {
throw new Error("Unable to save order");
}
}
If the save operation succeeds, commit the edit. If it fails, restore the previous value.
This gives users predictable feedback and avoids leaving the interface in an uncertain state.

  1. Support both local and remote data
    Not every screen needs the same data-loading strategy.
    A customer directory may work well with locally supplied records. An order management screen may need server-side paging, sorting, and filtering.
    A reusable grid should support both patterns without forcing every screen to implement its own data layer.
    For remote data, make sure the API understands the grid's paging, sorting, and filtering parameters. If grouping is performed across paginated data, the server must also return appropriately ordered records so groups are not split unexpectedly between pages.

  2. Use the same component across real screens
    Consider two examples:
    Orders screen
    Editable customer and amount fields
    Date and status columns
    Grouping by region
    Summary calculations
    Remote pagination
    API-based editing
    Customers screen
    Read-only customer information
    Country and customer-since columns
    Currency and boolean formatting
    Locally supplied records
    Both screens can use the same component. They simply provide different column definitions and data sources.
    That is the practical benefit of a reusable grid: screen-level code focuses on business requirements rather than repeating widget configuration.

  3. Consider Ext JS and ReExt for advanced grid requirements
    Building a reusable wrapper does not mean every advanced grid feature must be implemented from scratch.
    For teams using React alongside Ext JS, ReExt makes Ext JS components available within React applications.
    The Ext JS grid provides capabilities such as:
    Inline cell editing
    Grouping and summaries
    Paging
    Remote sorting
    Remote filtering
    A team can build a focused TypeScript component around these capabilities while keeping its own API simple.
    Existing Ext JS applications can also reuse custom grid classes through their xtype, which can be useful during gradual modernization.
    The right choice depends on your existing architecture, licensing requirements, team experience, and the grid capabilities your application needs.

  4. Test and document the shared component
    A reusable component becomes valuable only when developers can trust it.
    A practical testing strategy includes:
    Unit tests for column mapping and formatting.
    Tests for required-field validation and editor configuration.
    Browser tests for successful edits and failed-save rollbacks.
    Tests for grouping and summary calculations.
    Documentation with examples for local and remote data.
    Storybook or internal component documentation can help new developers understand the supported props without digging through the implementation.

Final thoughts
A reusable React grid component is more than a way to reduce duplicated JSX. It establishes shared rules for data presentation, editing, validation, loading, and error handling.
Start with a small API, centralize column behavior, keep configuration stable, and make the component easy to test and document.
For applications with demanding data-grid requirements, evaluating an established grid foundation such as Ext JS through ReExt may also reduce the amount of infrastructure your team needs to build and maintain.
Related reading: How to Build a Reusable React Grid Component
Tags: React, TypeScript, JavaScript, Web Development, Enterprise React

Top comments (0)