Skip to main content

CRUD Hooks

The hooks package in @repo/crud provides a collection of React hooks for performing CRUD operations in your components. These hooks are built on top of the CRUD Engine and provide a seamless integration with React.

Features

  • Data Fetching: Hooks for fetching data from your data provider
  • Data Mutations: Hooks for creating, updating, and deleting data
  • Loading States: Automatic handling of loading states
  • Error Handling: Consistent error handling across hooks
  • Optimistic Updates: Support for optimistic UI updates
  • Refetching: Automatic and manual refetching of data
  • TypeScript Support: Full TypeScript support with generics

Installation

This package is part of the zopio monorepo and is available to all applications in the workspace.

Basic Usage

Setting Up the Provider

Before using the hooks, you need to set up the CRUD provider in your application:

Data Fetching Hooks

useGetList

The useGetList hook fetches a list of resources from your data provider.

Options

useGetOne

The useGetOne hook fetches a single resource from your data provider.

Options

useGetMany

The useGetMany hook fetches multiple resources by their IDs.

Options

Data Mutation Hooks

useCreate

The useCreate hook creates a new resource in your data provider.

Options

useUpdate

The useUpdate hook updates an existing resource in your data provider.

Options

useDelete

The useDelete hook deletes an existing resource from your data provider.

Options

Advanced Hooks

useMutation

The useMutation hook provides a more flexible way to perform mutations.

Options

useQueryClient

The useQueryClient hook provides access to the query client for advanced use cases.

useCrudEngine

The useCrudEngine hook provides direct access to the CRUD engine.

Optimistic Updates

Many of the hooks support optimistic updates, which update the UI immediately before the server request completes:

TypeScript Support

All hooks are fully typed and support generics for better type safety:

Custom Hooks

You can create custom hooks that build on the provided hooks:

Pagination Hook

A custom hook for handling pagination:

API Reference

For a complete API reference, please refer to the TypeScript definitions in the package.