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-files-hooks

v1.0.4

Published

A React TypeScript package to download files

Downloads

31

Readme

use-export-files-hooks

use-export-files-hooks is a versatile npm package that provides two powerful custom hooks—useDownloadFile and useExportImage—for seamless file downloading and image exporting in React.js and Next.js applications. This package is designed to simplify and streamline the process of exporting files and images directly from your web applications.

Live Demo

For a hands-on demonstration, check out this CodeSandbox Example.

Key Features

  • Simple Integration: Easily integrate file downloading and image exporting functionalities into your React or Next.js projects.
  • Customizable: Hooks are designed to be flexible and can be adapted to various use cases.
  • Asynchronous Operations: Manage file downloads and image exports asynchronously with progress indicators.

Installation

To add use-export-files-hooks to your project, use npm:

npm install use-export-files-hooks

Usage

1. useDownloadFile Hook

The useDownloadFile hook simplifies the process of downloading files from a URL directly within your React components.

Parameters

  • requestFunction: A function returning a Promise that resolves to the file data.
  • name: The desired name for the downloaded file.

Returns

  • downloadFile: A function that triggers the file download.
  • isPending: A boolean indicating the download's progress status.

Example Usage

import React from 'react';
import { useDownloadFile } from 'use-export-files-hooks';

const MyComponent = () => {
  const requestFunction = () => fetch('https://jsonplaceholder.typicode.com/users');
  const { downloadFile, isPending } = useDownloadFile({ requestFunction, name: 'example-file' });

  return (
    <div>
      <button onClick={downloadFile} disabled={isPending}>
        {isPending ? 'Downloading...' : 'Download File'}
      </button>
    </div>
  );
};

export default MyComponent;

2. useExportImage Hook

The useExportImage hook allows you to export images of specific elements within your React components. Attach a ref to the element you wish to export and trigger the export function.

Returns

  • elementRef: A ref to be attached to the element intended for export.
  • exportElement: A function that initiates the image export.

Example Usage

import React from 'react';
import { useExportImage } from 'use-export-files-hooks';

const MyImageComponent = () => {
  const { elementRef, exportElement } = useExportImage();

  return (
    <div ref={elementRef}>
      <button onClick={() => exportElement("myImgName")}>Export Image</button>
      <h1>Hello</h1>
      <h3>Export this element</h3>
    </div>
  );
};

export default MyImageComponent;

Why Choose use-export-files-hooks?

  • Efficiency: Reduce development time by using pre-built hooks for common file operations.
  • Flexibility: Customize the hooks to fit your application's needs.
  • Community-Driven: Regular updates and community support to keep the package reliable and up-to-date.

Author

Developed by Ahmed Nasser. Feel free to connect on LinkedIn for more information or collaboration.