Skip to content

Commit 541bf3b

Browse files
committed
initial version
0 parents  commit 541bf3b

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Dependency directories
2+
node_modules/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# useImage React Hook
2+
3+
Custom React Hook for loading images.
4+
5+
## Install
6+
7+
```bash
8+
npm install use-image
9+
```
10+
11+
12+
## Usage
13+
14+
```js
15+
import React from 'react';
16+
import { Image } from 'react-konva';
17+
import useImage from 'use-image';
18+
19+
export default function App() {
20+
const url = 'https://cdn.int64ago.org/ogk39i54.png';
21+
let { image, status } = useImage(url);
22+
23+
// image is DOM image element or undefined
24+
// image will be undefined while its loading or loading is failed
25+
26+
// status can be "loading", "loaded" or "failed"
27+
28+
return (
29+
<Image image={image} />
30+
);
31+
}
32+
33+
```
34+
35+
## License
36+
37+
MIT

index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var React = require('react');
2+
3+
var defaultState = { image: undefined, status: 'loading' };
4+
5+
module.exports = function useImage(url) {
6+
var res = React.useState(defaultState);
7+
var image = res[0].image;
8+
var status = res[0].status;
9+
10+
var setState = res[1];
11+
12+
React.useEffect(
13+
function() {
14+
if (!url) return;
15+
const img = document.createElement('img');
16+
img.addEventListener('load', function() {
17+
setState({ image: img, status: 'loaded' });
18+
});
19+
img.addEventListener('error', function() {
20+
setState({ image: undefined, status: 'failed' });
21+
});
22+
img.src = url;
23+
},
24+
[url]
25+
);
26+
27+
// return array because it it better to use in case of several useImage hooks
28+
// const [background, backgroundStatus] = useImage(url1);
29+
// const [patter] = useImage(url2);
30+
return [image, status];
31+
};

package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "use-image",
3+
"version": "0.5.0",
4+
"description": "Custom React Hook for loading images.",
5+
"main": "index.js",
6+
"files": [
7+
"index.js"
8+
],
9+
"scripts": {},
10+
"repository": {
11+
"type": "git",
12+
"url": "https://github.com/konvajs/use-image.git"
13+
},
14+
"keywords": [
15+
"react",
16+
"react-hooks",
17+
"canvas",
18+
"image"
19+
],
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/konvajs/use-image/issues"
23+
},
24+
"homepage": "https://github.com/konvajs/use-image/",
25+
"peerDependencies": {
26+
"react": "^16.8.0",
27+
"react-dom": "^16.8.0"
28+
}
29+
}

0 commit comments

Comments
 (0)