React Blurhash Component

0.3.0 · active · verified Tue Apr 21

react-blurhash provides React components for efficiently displaying Blurhash placeholders in web applications. Blurhash is a compact algorithm for representing a blurred version of an image, enabling a smooth user experience by showing a low-resolution placeholder while the full image loads. Currently at version 0.3.0, the package offers two core components: `<Blurhash />` for a higher-level, self-scaling implementation, and `<BlurhashCanvas />` for direct canvas rendering, offering granular control over the decoding and display process. The library ships with TypeScript types, enhancing developer experience and type safety. It's built upon the core `blurhash` algorithm and requires `react` (>=15) and `blurhash` (^2.0.3) as peer dependencies, which must be installed separately. The package is actively maintained, though its pre-1.0 version number suggests the API might evolve in future major releases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to render a Blurhash placeholder using the `<Blurhash />` component. It illustrates basic usage with an example hash string and common props like width, height, and resolution. Remember to install `blurhash` and `react`.

import React from 'react';
import { Blurhash } from 'react-blurhash';

// Ensure you have installed peer dependencies: npm install blurhash react
// This component displays a Blurhash placeholder.
// In a real application, 'hash' would typically come from an image metadata API.

function ImagePlaceholder() {
  const exampleHash = "LEHV6nWB2yk8pyo0adR*.7kCMdnj"; // Example Blurhash string

  return (
    <div style={{ width: 400, height: 300, border: '1px solid #eee', overflow: 'hidden' }}>
      <h3>Blurhash Placeholder</h3>
      <Blurhash
        hash={exampleHash}
        width={400}
        height={300}
        resolutionX={32}
        resolutionY={32}
        punch={1}
      />
      <p>Content will load over this placeholder...</p>
    </div>
  );
}

export default ImagePlaceholder;

view raw JSON →