Skip to content

konvajs/use-image

Folders and files

NameName
Last commit message
Last commit date

Latest commit

May 19, 2025
547ca8a · May 19, 2025

History

49 Commits
Feb 6, 2019
Nov 17, 2022
May 5, 2023
Sep 6, 2022
May 19, 2025
May 19, 2025

Repository files navigation

useImage React Hook

Custom React Hook for loading images. It loads passed url and creates DOM image with such src. Useful for canvas application like react-konva.

Open image demo

Install

npm install use-image

Usage

import React from 'react';
import { Image } from 'react-konva';
import useImage from 'use-image';

const url = 'https://konvajs.github.io/assets/yoda.jpg';

function SimpleApp() {  
  const [image] = useImage(url);

  // "image" will be DOM image element or undefined

  return (
    <Image image={image} />
  );
}

function ComplexApp() {
  // set crossOrigin of image as second argument, set referrerpolicy as third argument
  const [image, status] = useImage(url, 'anonymous', 'origin');

  // status can be "loading", "loaded" or "failed"

  return (
    <Image image={image} />
  );
}

License

MIT