npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-export

v1.2.2

Published

The `useExport` hook is designed to make it easy to export data fetched from an API to an Excel file in a React application. It leverages `react-query` for API data management, `axios` for making requests, and custom utilities for exporting data to Excel.

Downloads

1,378

Readme

useExport

The useExport hook is designed to make it easy to export data fetched from an API to an Excel file in a React application. It leverages react-query for API data management, axios for making requests, and custom utilities for exporting data to Excel.

This documentation provides an overview of the useExport hook, including:

  • Installation: Instructions on how to install the useExport hook in your project.
  • Usage: Detailed guidance on how to use the hook within your React application.
  • Type Definitions: Definitions of the types used by the useExport hook for TypeScript support.
  • API Structure: Information on the API structure and parameters required for utilizing the hook effectively.

Features

  • Easy Integration: Seamlessly integrates with axios and react-query for fetching and managing data.
  • Excel Export: Allows exporting data to Excel files with customizable formatting and headers.
  • Customizable Data Handling: Format and filter data according to specific requirements before exporting.
  • Translation Support: Supports translation of DTO values for internationalization, allowing for localized data presentation in exported files.
  • Loading State: Provides a loading state to manage and display while exporting data.
  • Flexible Table Configuration: Supports flexible table header and body configuration, including data injection, translation, and date formatting.

Installation

To use the useExport hook, you need to install the following dependencies:

npm install use-export

Usage Example

Here’s an example of how to use the useExport hook in a React component:

import useExport from "use-export"; // Adjust the import path as necessary

const ExportComponent = () => {
  const { handleExport, isPending } = useExport({
    api: "/api/endpoint", // API endpoint to fetch the data
    params: { filter: "value" }, // Optional query parameters
    nameFile: "employees", // Name of the exported Excel file
    injection: "data", // (Optional) key to inject from the API response
    tableHeaderCellFiltered: {
      thead: ["ID", "Name", "Status", "Date"], // Headers for Excel
      tbody: [
        {
          injection: "id", // Key to extract ID
        },
        {
          injection: "name", // Key to extract Name
        },
        {
            injection: "status",
            translate: [
            // Optional translations for Name
            { if: "pending", else: "قيد المعالجة" },
            { if: "approved", else: "تم الموافقة عليها" },
          ],
        }
        {
          injection: "createdAt", // Key to extract Date
          isDate: true, // Format the value as a date
        },
      ],
    },
  });

  return (
    <button onClick={handleExport} disabled={isPending}>
      {isPending ? "Exporting..." : "Export Data"}
    </button>
  );
};

API Reference

useExport(options: UseExportOptions)

Parameters

| Option | Type | Required | Description | | ------------------------- | ----------------------------------- | -------- | -------------------------------------------------------------------------------------------------- | | api | string | Yes | The API endpoint to fetch the data for export. | | params | Record<string, number \| string > | No | Parameters to pass to the API request. | | nameFile | string | Yes | The name of the exported Excel file. | | injection | string | No | Key to extract a specific part of the API response into the export process. | | tableHeaderCellFiltered | TableHeadLess | Yes | Configuration for the table headers and body that define how the data should be exported to Excel. |

Return Values

  • handleExport: () => void
    Triggers the export process by making the API call, formatting the data, and exporting it as an Excel file.

  • isPending: boolean
    Indicates whether the export process is ongoing, useful for showing loading indicators or disabling buttons.

Type Definitions

// Configuration for table headers and data mapping
type TableHeadLess = {
  thead: string[]; // Array of table headers (column titles).
  tbody: {
    injection: string; // Key for data injection from API response.
    translate?: { id: string; else: string }[]; // Optional translations for the injected data.
    isDate?: boolean; // Optional flag to format the value as a date.
  }[];
};

// Options for the useExport hook
export interface UseExportOptions {
  api: string; // API endpoint to fetch data from.
  params?: Record<string, number | string>; // Optional query parameters.
  nameFile: string; // Name of the exported Excel file.
  injection?: string; // Optional key for extracting a specific part of the response.
  tableHeaderCellFiltered: TableHeadLess; // Configuration for table headers and body for Excel export.
}

// Hook return type
declare function useExport(options: UseExportOptions): {
  handleExport: () => void; // Function to trigger the export process.
  isPending: boolean; // Boolean indicating the pending state.
};

export default useExport;