diff --git a/examples/pagination-controlled/src/App.js b/examples/pagination-controlled/src/App.js index 4f90513..eaf5a5b 100644 --- a/examples/pagination-controlled/src/App.js +++ b/examples/pagination-controlled/src/App.js @@ -1,8 +1,8 @@ -import React from 'react' -import styled from 'styled-components' -import { useTable, usePagination } from 'react-table' +import React from "react"; +import styled from "styled-components"; +import { useTable, usePagination } from "react-table"; -import makeData from './makeData' +import makeData from "./makeData"; const Styles = styled.div` padding: 1rem; @@ -35,7 +35,7 @@ const Styles = styled.div` .pagination { padding: 0.5rem; } -` +`; // Let's add a fetchData method to our Table component that will be used to fetch // new data when pagination state changes @@ -45,7 +45,7 @@ function Table({ data, fetchData, loading, - pageCount: controlledPageCount, + pageCount: controlledPageCount }) { const { getTableProps, @@ -61,28 +61,30 @@ function Table({ nextPage, previousPage, setPageSize, + rows, // Get the state from the instance - state: { pageIndex, pageSize }, + state: { pageIndex, pageSize } } = useTable( { columns, data, - state: { pageIndex: 0 }, // Pass our hoisted table state + initialState: { pageIndex: 0 }, // Pass our hoisted table state manualPagination: true, // Tell the usePagination // hook that we'll handle our own data fetching // This means we'll also have to provide our own // pageCount. - pageCount: controlledPageCount, + pageCount: controlledPageCount }, usePagination - ) + ); // Now we can get our table state from the hoisted table state tuple // Listen for changes in pagination and use the state to fetch our new data React.useEffect(() => { - fetchData({ pageIndex, pageSize }) - }, [fetchData, pageIndex, pageSize]) + console.log(pageIndex); + fetchData({ pageIndex, pageSize }); + }, [fetchData, pageIndex, pageSize]); // Render the UI for your table return ( @@ -95,7 +97,7 @@ function Table({ pageSize, pageCount, canNextPage, - canPreviousPage, + canPreviousPage }, null, 2 @@ -107,7 +109,7 @@ function Table({ {headerGroups.map(headerGroup => ( {headerGroup.headers.map(column => ( - {column.render('Header')} + {column.render("Header")} ))} ))} @@ -119,18 +121,23 @@ function Table({ {row.cells.map(cell => { return ( - {cell.render('Cell')} - ) + {cell.render("Cell")} + ); })} ) )} - {loading ? ( - // Use our custom loading state to show a loading indicator - + + {loading ? ( + // Use our custom loading state to show a loading indicator Loading... - - ) : null} + ) : ( + + Showing {page.length} of ~{controlledPageCount * pageSize}{" "} + results + + )} + {/* @@ -139,39 +146,39 @@ function Table({ */}
{' '} + {"<<"} + {" "} {' '} + {"<"} + {" "} {' '} + {">"} + {" "} {' '} + {">>"} + {" "} - Page{' '} + Page{" "} {pageIndex + 1} of {pageOptions.length} - {' '} + {" "} - | Go to page:{' '} + | Go to page:{" "} { - const page = e.target.value ? Number(e.target.value) - 1 : 0 - gotoPage(page) + const page = e.target.value ? Number(e.target.value) - 1 : 0; + gotoPage(page); }} - style={{ width: '100px' }} + style={{ width: "100px" }} /> - {' '} + {" "}
- ) + ); } // Let's simulate a large dataset on the server (outside of our component) -const serverData = makeData(10000) +const serverData = makeData(10000); function App() { const columns = React.useMemo( () => [ { - Header: 'Name', + Header: "Name", columns: [ { - Header: 'First Name', - accessor: 'firstName', + Header: "First Name", + accessor: "firstName" }, { - Header: 'Last Name', - accessor: 'lastName', - }, - ], + Header: "Last Name", + accessor: "lastName" + } + ] }, { - Header: 'Info', + Header: "Info", columns: [ { - Header: 'Age', - accessor: 'age', + Header: "Age", + accessor: "age" }, { - Header: 'Visits', - accessor: 'visits', + Header: "Visits", + accessor: "visits" }, { - Header: 'Status', - accessor: 'status', + Header: "Status", + accessor: "status" }, { - Header: 'Profile Progress', - accessor: 'progress', - }, - ], - }, + Header: "Profile Progress", + accessor: "progress" + } + ] + } ], [] - ) + ); // We'll start our table without any data - const [data, setData] = React.useState([]) - const [loading, setLoading] = React.useState(false) - const [pageCount, setPageCount] = React.useState(0) + const [data, setData] = React.useState([]); + const [loading, setLoading] = React.useState(false); + const [pageCount, setPageCount] = React.useState(0); const fetchData = React.useCallback(({ pageSize, pageIndex }) => { // This will get called when the table needs new data @@ -240,21 +247,21 @@ function App() { // even a server. But for this example, we'll just fake it. // Set the loading state - setLoading(true) + setLoading(true); // We'll even set a delay to simulate a server here setTimeout(() => { - const startRow = pageSize * pageIndex - const endRow = startRow + pageSize - setData(serverData.slice(startRow, endRow)) + const startRow = pageSize * pageIndex; + const endRow = startRow + pageSize; + setData(serverData.slice(startRow, endRow)); // Your server could send back total page count. // For now we'll just fake it, too - setPageCount(Math.ceil(serverData.length / pageSize)) + setPageCount(Math.ceil(serverData.length / pageSize)); - setLoading(false) - }, 1000) - }, []) + setLoading(false); + }, 1000); + }, []); return ( @@ -266,7 +273,7 @@ function App() { pageCount={pageCount} /> - ) + ); } -export default App +export default App;