react-hook-table
is a hook with a set of tools to help to control a table. The hook provides control over sorting, pagination and filtering while updating the url search params to maintain consistency over page updates.
- Uses @resourge/react-fetch
- Saves filters, pagination and sorting on search params.
- Provides a set of methods to help maintain and control a table pagination, sorting and filtering.
- Provides methods to connect the navigation with other packages. (ex: react-router)
- parseParams, parseSearch method's that extend URLSearchParams for own use.
- useInfiniteLoading
Install using Yarn:
yarn add @resourge/react-hook-table
or NPM:
npm install @resourge/react-hook-table --save
By default, @resourge/react-hook-table
uses native javascript history to update the URL. This can create a problem with packages that don't connect to native javascript history, for example react-router
.
To fix this do:
import { createBrowserHistory } from 'history';
export const history = createBrowserHistory();
// This functions connects the @resourge/react-hook-table to the react-router history
setDefaultNavigation(({ location }) => history.replace(location.path))
This connects react-router
navigation with @resourge/react-hook-table
import React from 'react';
import { usePagination } from '@resourge/react-hook-table';
export default function List() {
const {
data: products, // Variable containing 'data'
filter,
error,
isLoading,
pagination,
sort,
fetch, // Method to re-fetch
setFilter,
changeItemsPerPage,
changePage,
changePagination,
changeTotalPages,
getPaginationHref,
reset,
resetPagination,
setPaginationState,
sortTable
} = usePagination(
async ({ pagination, sort, filter }) => {
// fetch... use Http
return await Promise.resolve({
data: [
{ id: Math.random(), name: 'Apple' }
],
totalItems: 10
})
},
{
initialState: [],
/*
// Default values
filter,
sort,
pagination
*/
deps: [] // Extra dependencies
}
)
return (
<table>
<tbody>
{
products.map((product, index) => (
<tr key={`${index}`}>
<td>{ product.id } </td>
<td>{ product.name } </td>
</tr>
))
}
</tbody>
</table>
)
}
Name | Type | Required | Description |
---|---|---|---|
fetchMethod | Promise |
true | Method that return the Data and totalItems. |
config | { initialState: any, filter?: object, hash?: boolean, pagination?: PaginationCriteria, sort?: SortCriteria } & UseFetchConfig |
true | Config to set initial/default filter, pagination and sort and initialState. When hash is true it will use Location hash instead of Location pathname to get `search. UseFetchConfig from @resourge/react-fetch. |
Pagination Filters
Pagination per page and items per page
Pagination sort
Method to refetch the current pagination
const {
...
fetch
} = usePagination(...)
Method to updates filters.
const {
...
setFilter
} = usePagination(...)
Changes items per page
const {
...
changeItemsPerPage
} = usePagination(...)
Changes current page
const {
...
changePage
} = usePagination(...)
Changes both current page and items per page
const {
...
changePagination
} = usePagination(...)
Changes total number of pages using total number of items Note: It doesn't trigger render.
const {
...
changeTotalPages
} = usePagination(...)
Builds href for use on navigation. (usually used with pagination component)
const {
...
getPaginationHref
} = usePagination(...)
Resets the pagination, sort and/or filter.
const {
...
reset
} = usePagination(...)
Resets pagination to initial/default values
const {
...
resetPagination
} = usePagination(...)
To set pagination state manually
const {
...
setPaginationState
} = usePagination(...)
Changes which column to order asc/desc.
const {
...
sortTable
} = usePagination(...)
import React from 'react';
import { useInfiniteLoading, RefreshControl } from '@resourge/react-hook-table';
export default function List() {
const {
data: products, // Variable containing 'data'
filter,
error,
isLoading,
sort,
context,
isLast,
isLastIncomplete,
loadMore,
preload,
fetch, // Method to re-fetch
setFilter,
changeItemsPerPage,
reset,
setPaginationState,
sortTable
} = useInfiniteLoading(
async ({ pagination, sort, filter }) => {
// fetch... use Http
return await Promise.resolve({
data: [
{ id: Math.random(), name: 'Apple' }
],
totalItems: 10
})
},
{
initialState: [],
/*
// Default values
filter,
sort,
pagination
*/
deps: [] // Extra dependencies
}
)
return (
<table>
<tbody>
{
products.map((product, index) => (
<tr key={`${index}`}>
<td>{ product.id } </td>
<td>{ product.name } </td>
</tr>
))
}
<tr>
<td>
<RefreshControl
context={context}
renderComponent={({ isLastIncomplete, onClick }) => (
!isLastIncomplete
? (
<button
disabled={isLoading}
onClick={onClick}
>Get more</button>
) : null
)}
/>
</td>
</tr>
</tbody>
</table>
)
}
Contains all the from useInfiniteLoading. (Works with RefreshControl)
const {
...
loadMore
} = usePagination(...)
If is last 'page'
const {
...
isLast
} = usePagination(...)
If last 'page' is incomplete (itemPerPage 10 but the last page got less than 10)
const {
...
isLastIncomplete
} = usePagination(...)
Method to load more 'pages'
const {
...
loadMore
} = usePagination(...)
Method to preload the next 'page
const {
...
preload
} = usePagination(...)
Method to restore scroll in infinite scroll.
// useAction will probably be from a navigation/router package
// Ex: import { useAction } from '@resourge/react-router';
const action = useAction();
// 'action' must be 'pop' for restoration to work;
const [scrollRestoration, ref] = useInfiniteScrollRestoration(action);
const {} = useInfiniteLoading(
...,
{
initialState: [],
scrollRestoration
}
);
Component to help useInfiniteScroll control the scroll.
import React from 'react';
import { useInfiniteLoading, RefreshControl } from '@resourge/react-hook-table';
export default function List() {
const {
//...
context,
} = useInfiniteLoading(
async ({ pagination, sort, filter }) => {
// fetch... use Http
return await Promise.resolve({
data: [
{ id: Math.random(), name: 'Apple' }
],
totalItems: 10
})
},
{
initialState: [],
deps: [] // Extra dependencies
}
)
return (
<>
...
<RefreshControl
context={context}
renderComponent={({ isLastIncomplete, onClick }) => (
!isLastIncomplete
? (
<button
disabled={isLoading}
onClick={onClick}
>Get more</button>
) : null
)}
/>
</>
)
}
Params object into search path
import { parseParams } from '@resourge/react-hook-table';
parseParams({
productId: 10,
productName: 'Apple'
})
// ?productId=10&productName=Apple
Converts search string into object.
import { parseSearch } from '@resourge/react-hook-table';
parseSearch('?productId=10&productName=Apple')
// {
// productId: 10,
// productName: 'Apple'
// }
MIT Licensed.