useDownload
A hook that provides file download functionality with progress tracking and error handling. Perfect for implementing download managers and file handling interfaces.
Parameters
This hook doesn’t take any parameters.
Returns
error:Error | unknown | null- Error information if download failsisDownloading:boolean- Whether a download is in progressprogress:number | null- Download progress percentage (0-100)downloadFile:(fileName: string, fileUrl: string) => Promise<void>- Function to start a file download
Usage
import { useDownload } from '@rhinolabs/react-hooks';
function FileDownloader() { const { error, isDownloading, progress, downloadFile } = useDownload();
const handleDownload = () => { downloadFile( 'example.pdf', 'https://example.com/files/document.pdf' ); };
return ( <div> <button onClick={handleDownload} disabled={isDownloading} > {isDownloading ? 'Downloading...' : 'Download File'} </button>
{progress !== null && ( <div>Progress: {progress}%</div> )}
{error && ( <div style={{ color: 'red' }}> Download failed: {error.message} </div> )} </div> );}Notes
- Supports progress tracking
- Handles download errors
- Uses Blob URLs for efficiency
- Cleans up after download
- Supports large files
- Progress percentage updates
- Automatic error handling
- Downloads via native browser dialog