Custom React Hook

useFetch

useFetch is built specifically for antd-dynamic-ui components. It tracks loading and error states, automatically cancels in-flight requests when query parameters or URLs change via AbortController, and provides a stable refetch() callback for manual data refreshing.

Built-in AbortController request cancellation prevents out-of-order responses and race conditions
Automatic payload error detection (detects { success: false, message })
Manual execution mode (dontCall: true) for submit buttons and search triggers
Configurable onSuccess callback with guaranteed latest closure
Zero re-render memoization for request options and body payloads

Hook Parameters

PropertyTypeDefaultDescription
clientRequiredVelocityMethod | AxiosInstance—HTTP client instance (e.g. api.get from antd-dynamic-ui).
urlRequiredstring—Endpoint URL to query.
optionsVelocityConfig—Query params, headers, timeout, and custom request configs.
bodyany—Request payload body for POST / PUT requests.
dontCallbooleanfalseIf true, does not fetch automatically on mount (manual execution mode).
onSuccess(data: R) => void—Callback triggered upon successful response.

Return Object Properties

PropertyTypeDefaultDescription
dataR | null—Parsed response data object.
loadingboolean—Whether request is in-flight.
errorstring | null—Human-readable error message if failed.
refetch(overrides?: any) => Promise<void>—Function to trigger a re-fetch with optional param overrides.

Practical Hook Examples & Workflows

Explore quickstart usage, full parameter integration, and production data workflows.

Auto-Fetch on Component Mount: Standard declarative data fetch on mount with loading and error boundaries.
useFetch.mount.tsxtsx
1import React from "react";
2import { useFetch, api, LoadingSpinner, ErrorApi } from "antd-dynamic-ui";
3import { Card } from "antd";
4
5export default function AnalyticsCard() {
6 const { data, loading, error, refetch } = useFetch({
7 client: api.get,
8 url: "/api/v1/dashboard/metrics",
9 options: { params: { range: "30d" } },
10 });
11
12 if (loading) return <LoadingSpinner size="medium" tip="Loading KPIs..." />;
13 if (error) return <ErrorApi title="Metrics Error" message={error} onRetry={refetch} />;
14
15 return (
16 <Card title="Monthly Performance">
17 <div className="text-2xl font-bold font-mono">
18 ${(data as any)?.revenue?.toLocaleString() || "0.00"}
19 </div>
20 </Card>
21 );
22}

Expected API Response Structure

Expected API Response Payload

Your backend API must return JSON matching this exact structure.

{
  "success": true,
  "message": "Fetched successfully",
  "data": {
    "rows": [
      {
        "id": 1,
        "name": "Alice Johnson"
      }
    ],
    "totalRows": 1
  }
}