Skip to Content
FrontendBundlesCRUDContextsListing Context

Listing Context

ListingContext is the central state manager for all CRUD listing pages. It manages API data fetching, pagination, sorting, filter state, and URL query string synchronization.


Import

import { ListingContext } from "@phpcreation/frontend-crud-react-nextjs-bundle/contexts/ListingContext";

What It Manages

StateDescription
dataCurrent page of entities from the API
totalTotal entity count
pageCurrent page number
perPagePage size
sortSort column and direction
filtersActive filter values
loadingLoading state
errorError state

Reordering handlers

Row reordering lives in this context, used by the ListingActions move buttons:

HandlerWhat it does
handleMoveItem(id, direction, neighborId)Swaps the priority values of the row and its adjacent row (two PATCH requests), then swaps them in local state.
handleTreeMoveItem(id, direction)Swaps lft/rgt with the adjacent same-parent sibling (two PATCH requests), then refetches. Used on the tree view route.
moveLoadingIds / treeMoveLoadingIdsIds with an in-flight move, used to show the button spinners.

Usage

Wrap all listing components in ListingContext:

import { ListingContext } from "..."; import { ListingTable } from "..."; import { ListingFilters } from "..."; import { ListingPagination } from "..."; <ListingContext entityConfig={entityConfig}> <ListingFilters filterFields={filterFields} /> <ListingTable columns={columns} actions={actions} /> <ListingPagination /> </ListingContext>

useListingContext Hook

Access context state in child components:

import { useListingContext } from "@phpcreation/frontend-crud-react-nextjs-bundle/contexts/ListingContext"; function MyComponent() { const { data, loading, setFilters, setPage } = useListingContext(); // ... }

Entity Config

Pass your entity’s API config to the context:

export const entityConfig = { apiUrl: "/api/invoices", defaultSort: { field: "createdAt", direction: "desc" }, defaultPerPage: 25, };

ListingContext syncs all state to the URL query string automatically via the Query String module. No extra setup is needed.


Last updated on