question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useImage React hook

See original GitHub issue

I am thinking to implement useImage hook with the simple API:

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

export default function App() {
  const url = 'some-image-url';
  const image = useImage(url);
   // it will be undefined while image is loading
   // and it will be instance of window.Image when it is loaded


  return (
    <Stage width={300} heihgt={300}>
        <Layer>
            <Image image={image} />
        </Layer>
    </Stage>
  );
}

That hook will simplify current image usage https://konvajs.github.io/docs/react/Images.html

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
levrikcommented, Jan 28, 2019

Why don’t implement a better Image component for that? I use the following code in my application.

class UrlImage extends Component {
  static propTypes = {
    imageUrl: PropTypes.string.isRequired
  }

  state = {
    image: null
  }

  componentDidMount() {
    this.loadImage();
  }

  componentDidUpdate(prevProps) {
    if (prevProps.imageUrl !== this.props.imageUrl) {
      this.loadImage();
    }
  }

  loadImage = () => {
    const image = new window.Image();
    image.src = this.props.imageUrl;
    image.onload = () => {
      this.setState({
        image
      });
    }
  }

  render() {
    const { imageUrl, ...props } = this.props;
    return <Image image={this.state.image} {...props} />
  }
}
0reactions
lavrtoncommented, Feb 6, 2019

https://github.com/konvajs/use-image

export default function App() {
  const url = 'https://konvajs.github.io/assets/yoda.jpg';
  const [image] = useImage(url);

  return (
    <Image image={image} />
  );
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

konvajs/use-image: Custom React Hook for loading images
Custom React Hook for loading images. It loads passed url and creates DOM image with such src . Useful for canvas application like...
Read more >
use-image
Custom React Hook for loading images.. Latest version: 1.1.0, ... Start using use-image in your project by running `npm i use-image`.
Read more >
useImageOnLoad() react hook
A simple React Hook that helps you improve UX while images are loading. Rather than having an image that "unfolds" from top to...
Read more >
Rendering images smoothly using React Hooks
In this article I'm going to create very simple Hook to render images smoothly on the screen (Not really rendering but sort of...
Read more >
Use-image NPM
useImage React Hook. Custom React Hook for loading images. It loads passed url and creates DOM image with such src . Useful for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found