Skip to content

Commit a856750

Browse files
authored
fix(onLoad): handle pass in new image source (#4)
* fix(onLoad): handle pass in new image source * update
1 parent a470d1a commit a856750

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import ImageOnLoad from 'react-image-onload';
4141

4242
```js
4343
type Props = {
44+
src: string,
4445
onLoad: (image: HTMLImageElement) => Promise<void> | void,
4546
};
4647
```

src/ImageOnLoad.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import omit from 'ramda/src/omit';
55
import PropTypes from 'prop-types';
66

77
export type Props = {
8+
src: string,
89
onLoad: (image: HTMLImageElement) => Promise<void> | void,
910
};
1011

1112
class InputOnLoad extends React.Component<Props> {
1213
static propTypes = {
14+
src: PropTypes.string,
1315
onLoad: PropTypes.func.isRequired, // (image: HTMLImageElement) => Promise<void> | void,
1416
};
1517
componentDidMount() {
@@ -21,6 +23,11 @@ class InputOnLoad extends React.Component<Props> {
2123
onLoad(image);
2224
}
2325
}
26+
componentWillReceiveProps(nextProps: Props) {
27+
if (nextProps.src !== this.props.src) {
28+
this.isLoaded = false;
29+
}
30+
}
2431
componentWillUnmount() {
2532
this.isLoaded = false;
2633
}
@@ -37,7 +44,7 @@ class InputOnLoad extends React.Component<Props> {
3744
props: Props;
3845
render() {
3946
const { onRef, onLoad } = this;
40-
const otherProps = omit(['onLoad'])(this.props);
47+
const otherProps: Object = omit(['onLoad'])(this.props);
4148

4249
return <img ref={onRef} onLoad={onLoad} {...otherProps} />;
4350
}

0 commit comments

Comments
 (0)