Supercharge Your React Apps: Declarative Client Downloads and Desktop Notifications

Enhancing web application interactivity often involves direct interaction with native browser capabilities. Common tasks like exporting JSON reports or sending native OS alerts usually force developers to craft imperative DOM manipulations, handle dynamic Blob object URLs, or coordinate web browser permissions.

The latest release of react-hook-lab solves these challenges by introducing two production-ready hooks: useDownload and useNotifications.

1. Effortless Client Data Exports with useDownload

The new useDownload hook simplifies client-side file downloading. It accepts plain text strings, JavaScript objects (auto-converted to JSON), Blobs, or remote URLs. It tracks download statuses (idle, downloading, success, error) and automatically cleans up object URLs to prevent browser memory leaks.

Example: Exporting Data with useDownload

import React from "react";
import { useDownload } from "react-hook-lab";

export function DataExporter() {
  const { download, status, error } = useDownload();

  const exportReport = () => {
    const reportData = { summary: "Quarterly Growth", score: 98 };
    download(reportData, "quarterly-report.json");
  };

  return (
    <div>
      <button onClick={exportReport} disabled={status === "downloading"}>
        {status === "downloading" ? "Preparing File..." : "Download Report"}
      </button>
      {error && <p style={{ color: "red" }}>{error}</p>}
    </div>
  );
}

2. Browser Desktop Alerts with useNotifications

Sending native desktop alerts requires managing browser permission lifecycle states (default, granted, denied) and handling cleanup. useNotifications provides an intuitive interface to handle permission prompts automatically or on demand, manage browser focus changes, and dismiss open notifications when components unmount.

Example: Triggering System Alerts

import React from "react";
import { useNotifications } from "react-hook-lab";

export function SystemAlert() {
  const { sendNotification, requestPermission, permission } = useNotifications();

  const notifyUser = async () => {
    if (permission !== "granted") {
      await requestPermission();
    }
    sendNotification("New Message Received", {
      body: "You have received a new update in your inbox.",
    });
  };

  return (
    <div>
      <button onClick={notifyUser}>Send Local Notification</button>
    </div>
  );
}

Summary of What Changed

  • useDownload: Added dynamic file downloading for JSON, text, Blobs, and remote HTTP resources.
  • useNotifications: Added complete notification permission state management, desktop notification creation, and automatic event teardown.

Resources & Links

Comments

Popular posts from this blog

Stop Guessing: Diagnosing React Re-Renders with the New useRenderReason Hook

Multi-Tab State Sync Made Easy: Introducing useSharedState in react-hook-lab!

Debug React Context Renders: The Latest Update to useRenderReason