69Implement server-side pagination with sorting and filtering
mediumAbout 40 minutesCodingreactpaginationtablesrace-conditions
Problem
Build <UsersTable fetchUsers={fetchUsers} pageSize={10} />. The server does the work: fetchUsers({ page, pageSize, sortKey, sortDir, filter }) resolves to { rows, total }, where rows are { id, name, email }.
Render:
<input aria-label="Filter">;- a
<table>whoseNameandEmailheaders contain a<button>that sorts by that column, witharia-sort="ascending" | "descending" | "none"on the<th>; - one
<tr>per row in<tbody>; <span>Page {page} of {pages}</span>with Previous and Next buttons, disabled at the ends.
Requirements
- Start with page 1, sorted by
nameascending, no filter. - Clicking a header sorts by that column ascending; clicking it again flips the direction. Any sort change goes back to page 1.
- Changing the filter goes back to page 1.
- Keep the old rows on screen while the next page loads (no empty flash), and mark the table with
aria-busy="true". - Ignore responses for older requests.
Try it yourself
Write your solution, run it, then submit to check it against the tests.
App.jsxReact + DOM
Step-by-step
{ page, sortKey, sortDir, filter } describes exactly which data you want. Changing any part of it is one setQuery call, so "change the filter and reset the page" happens in a single update.
Unlock the full solution
What's inside
- Step-by-step
- Approach
- Solution
- Complexity
- Edge cases
- Follow-up questions
- Tests
Includes 2 code listings, 4 edge cases, 4 follow-up questions.
One-time payment by UPI, card or net banking. Launch price.