Skip to content

besotti/react-async-csv-downloader

Repository files navigation

npm version License

A simple and user-friendly React Hook library that enables asynchronous downloading of files, particularly CSV files. This library is ideal for applications that dynamically generate and download data.

Table of Contents

Installation

To install the library in your project, run the following command in your terminal:

npm install react-async-csv-downloader

Usage

To use the library, import the useDownloadLink hook and implement it in your component. Here’s a basic example demonstrating how to use the hook effectively.

Examples

Basic Example

import React from 'react';
import { useDownloadLink } from 'react-async-csv-downloader';
import { RawCSVExport } from './types';

const MyComponent = () => {
    const asyncResolver = async () => {
        // Simulate an asynchronous data fetch, e.g., from an API
        const response = await fetch('/api/data');
        const data = await response.json();
        
        return {
            file: new Blob([data], { type: 'text/csv' }),
            type: 'text/csv',
        } as RawCSVExport;
    };

    const { getProps } = useDownloadLink({
        asyncResolver,
        onStart: () => console.log('Download started...'),
        onSuccess: () => console.log('Download successful!'),
        onError: (error) => console.error('Download error:', error),
    });

    return (
        <a {...getProps()} download="data.csv">
            Download CSV
        </a>
    );
};

export default MyComponent;

API

useDownloadLink

const { getProps } = useDownloadLink({
    asyncResolver: () => Promise<RawCSVExport>;
    onStart?: () => void;
    onSuccess?: () => void;
    onError?: (error: Error) => void;
});
  • asyncResolver: A function that resolves the data for download asynchronously and returns a Promise<RawCSVExport>.
  • onStart: (optional) A callback function that is called when the download starts.
  • onSuccess: (optional) A callback function that is called when the download is successful.
  • onError: (optional) A callback function that is called when an error occurs during the download.

Types

export type RawCSVExport = {
    file: Blob;
    type: string;
};

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please open an issue or a pull request if you have suggestions for improvements or new features.

Acknowledgments

  • Thanks to the Open Source community for their support and inspiration.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published