fix: should show children if src is not exist, close: #6804

This commit is contained in:
Benjy Cui 2017-07-14 16:10:52 +08:00
parent 02fa2cd672
commit b45bf6d814

View File

@ -31,6 +31,7 @@ export default class Avatar extends React.Component<AvatarProps, any> {
super(props);
this.state = {
scale: 1,
isImgExist: true,
};
}
@ -63,6 +64,8 @@ export default class Avatar extends React.Component<AvatarProps, any> {
}
}
handleImgLoadError = () => this.setState({ isImgExist: false });
render() {
const {
prefixCls, shape, size, src, icon, className, ...others,
@ -80,8 +83,13 @@ export default class Avatar extends React.Component<AvatarProps, any> {
});
let children = this.props.children;
if (src) {
children = <img src={src} />;
if (src && this.state.isImgExist) {
children = (
<img
src={src}
onError={this.handleImgLoadError}
/>
);
} else if (icon) {
children = <Icon type={icon} />;
} else {