Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ var ImageLoader = function (_React$Component) {

var _this = _possibleConstructorReturn(this, (ImageLoader.__proto__ || Object.getPrototypeOf(ImageLoader)).call(this, props));

_this.state = { status: props.src ? Status.LOADING : Status.PENDING };
_this.state = {
status: props.src ? Status.LOADING : Status.PENDING,
src: props.src
};
if (_react2.default.Children.count(props.children) !== 3) console.error('wrong # of children provided to ImageLoader');
return _this;
}
Expand All @@ -51,15 +54,6 @@ var ImageLoader = function (_React$Component) {
this.createLoader();
}
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if (this.props.src !== nextProps.src) {
this.setState({
status: nextProps.src ? Status.LOADING : Status.PENDING
});
}
}
}, {
key: 'componentDidUpdate',
value: function componentDidUpdate() {
Expand Down Expand Up @@ -140,6 +134,17 @@ var ImageLoader = function (_React$Component) {
(this.state.status === Status.LOADING || this.state.status === Status.PENDING) && childrenArray[2]
);
}
}], [{
key: 'getDerivedStateFromProps',
value: function getDerivedStateFromProps(nextProps, prevState) {
if (prevState.src !== nextProps.src) {
return {
status: nextProps.src ? Status.LOADING : Status.PENDING,
src: nextProps.src
};
}
return null;
}
}]);

return ImageLoader;
Expand Down
15 changes: 10 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export default class ImageLoader extends React.Component {

constructor(props) {
super(props);
this.state = {status: props.src ? Status.LOADING : Status.PENDING};
this.state = {
status: props.src ? Status.LOADING : Status.PENDING,
src: props.src
};
if(React.Children.count(props.children) !== 3)
console.error('wrong # of children provided to ImageLoader')
}
Expand All @@ -31,12 +34,14 @@ export default class ImageLoader extends React.Component {
}
}

componentWillReceiveProps(nextProps) {
if (this.props.src !== nextProps.src) {
this.setState({
static getDerivedStateFromProps(nextProps, prevState) {
if (prevState.src !== nextProps.src) {
return {
status: nextProps.src ? Status.LOADING : Status.PENDING,
});
src: nextProps.src
};
}
return null;
}

componentDidUpdate() {
Expand Down