How would you build a data table with sorting & filtering in React?
Design a performant, accessible data table with client-side sorting, column filtering, search, pagination, and memoization.
advancedUi patternsreactdata tablesortingfiltering
Published: 11/19/2025
Updated: 11/19/2025
Advertisement
đź§© Scenario
Create a reusable data table component that supports:
- Column sorting (asc/desc/none)
- Global search (debounced)
- Column filters (e.g., status, date range)
- Pagination (client-side)
- Performance optimizations (memoization, virtualization note)
- Accessibility (keyboard focus, ARIA attributes)
đź§ Answer (Design Overview)
Key Decisions
- Client vs Server: Choose based on dataset size. We'll demo client-side behavior for simplicity.
- Immutable Data Flow: Keep original data and derive visible rows using memoized selectors (
useMemo). - Sorting Strategy: Store
sortBy: { key, direction }. Toggling cycles throughasc -> desc -> none. - Filtering Strategy: Keep a
filtersobject per column and aglobalQueryfor full-text search. - Pagination: Apply after filtering & sorting. Use page size and current page state.
- Virtualization: For very large tables, integrate
react-windowafter implementing sorting/filtering.
Tradeoffs:
- Client-side filtering is fast for small datasets but memory-heavy for large ones.
- Complex multi-column sorts may require stable sort implementations.
🎮 Live Demo: Data Table with Sorting & Filtering
🔍 Real-World Tips
- Add column resizing and column visibility toggles for power users.
- Use server-side cursors and filtering for scalable apps.
- Use a stable sort to ensure deterministic ordering when values are equal.
- Cache filtered results when filters are expensive.
đź§ Diagram
flowchart TD A[Raw Data] --> B[Global Search Filter] B --> C[Column Filters] C --> D[Sorting] D --> E[Pagination] E --> F[Rendered Rows]
Quick Practice
- Implement column visibility toggles and remember preferences in localStorage.
- Add export CSV for the current filtered view.
- Replace client-side pagination with server-side and update UI accordingly.
Summary
- Use
useMemoto avoid recalculating filtered/sorted rows unnecessarily. - Decide client vs server strategy based on data size.
- For very large tables use virtualization libraries and server-side queries.
Frequently Asked Questions
Should I implement sorting & filtering on client or server?
For small datasets (<10k) client-side is fine. For large datasets or when using complex queries, do server-side sorting/filtering.
How to handle many columns and complex filters?
Provide column visibility toggles and a filter builder UI or use a server-side query builder.
Advertisement
Stay Updated
Get the latest frontend challenges, interview questions and tutorials delivered to your inbox.
Advertisement