How chiazhe/tan-stack-table works
Project Type and Purpose
• This is a full-stack demo application illustrating server-side pagination, column filtering, and sorting using TanStack Table (React Table) and TanStack Query (React Query).
• It comprises three main parts:
• A lightweight Express.js backend serving mock user data from a JSON file.
• A ReactJS (Vite) frontend.
• A Next.js frontend (App Router).
File Structure Breakdown
• Root
– README.md (overview & setup)
– backend/ (Express API)
– frontend-nextjs/ (Next.js App Router project)
– frontend/ (ReactJS + Vite project)
• backend/
– server.js: Express server exposes REST endpoint (e.g., GET /users) that implements pagination, filtering, sorting based on query parameters.
– MOCK_DATA.json: array of user objects (simulates database).
– package.json: Node dependencies (express, cors, etc.)
• frontend-nextjs/
– app/: Next.js App Router pages and layout (page.tsx fetches data).
– api/useGetUsers.ts: custom hook wrapping TanStack Query to call the backend.
– components/: TanStack Table components (basic table, filters, pagination, sorting) + shared UI primitives (button, input, table, pagination controls).
– hooks/useDebounce.ts, lib/utils.ts, types/*.ts, Tailwind + PostCSS config.
• frontend/
– src/main.tsx: ReactDOM render.
– src/api/useGetUsers.ts: hook using TanStack Query.
– src/components/: identical TanStack Table components & UI primitives.
– src/hooks/useDebounce.ts, src/lib/utils.ts, src/types/*.ts, Vite config, Tailwind.
Key Architectural Patterns
• Client-Server separation: React/Next frontends communicate via REST API.
• Reusable UI and table-concern components: All table logic broken into composable components (sorting, filtering, pagination).
• Single source of truth for data fetching: TanStack Query handles caching, refetching, and query key management.
• Configuration: Tailwind CSS for styling, TypeScript for typesafety.
A. Data Layer
• MOCK_DATA.json (simulated database)
B. Backend/API Layer
• Express Server (server.js)
• GET /users endpoint (accepts page, size, sort, filter queries)
C. Frontend Applications (side by side or as interchangeable clients)
1. ReactJS (Vite) Client
• API Hook (useGetUsers)
• TanStack Query Client
• Table Components (BasicTable, Sorting, Filtering, Pagination)
• UI Primitives (Button, Input, Table, PaginationControls)
2. Next.js Client
• App Router page (fetches data in React server/client component)
• API Hook (useGetUsers)
• Same TanStack Table Components & UI Primitives
D. External / Dev Tools (optional)
• TanStack Table & Query libraries
• Tailwind CSS
• Vite and Next.js build pipelines
Relationships and Data Flow
1. On user interaction (change page, sort, filter), the frontend hook constructs a query.
2. TanStack Query issues HTTP GET /users?page=X&limit=Y&sort=…&filter=… to Express server.
3. Express server reads MOCK_DATA.json, applies pagination, sorting, filtering logic, then returns JSON.
4. TanStack Query receives response, caches results, triggers UI rerender in Table components.