Given a list of users, build a user data table that displays users in a paginated format.
Data tables are frequently asked about during front-end interviews, as displaying paginated data with filtering functionality is a UI pattern built at almost every company.
Since the table skeleton has been provided, we can focus on the state and data manipulation aspects of the data table.
State is straightforward. Only two state values are needed: the current page and the page size. Since the data does not change in this case, there's no need for the user data to be part of state.
These state values are manipulated by the page size <select> and prev/next buttons.
The maximum number of pages can be derived from the number of users divided by the page size, so it does not need to be part of state.
We implement a function paginateUsers that takes in the list of users, the page number, and the page size. It will return the list of users for the current page and the total number of pages.
To determine the list of users for the current page, we can determine the start and end indices, then use Array.prototype.slice() to extract the appropriate slice from the users list:
Array.prototype.slice(), it doesn't matter if the end index exceeds the size of the list.paginateUsers() will be called in the render path, and the returned pageUsers array contains the current page of users to be rendered. The rendering code doesn't need to be changed much.
Some other user experience improvements we can make:
<!doctype html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /></head><body><div id="root"></div></body></html>
console.log() statements will appear here.