我有一个react组件,它是列表中的详细视图。
如果图像不存在并且出现 404 错误,我会尝试用默认图像替换图像。
我通常会在 img 标签中使用 onerror 方法,但这似乎不起作用。
我不知道如何通过react来做到这一点。
这是我的组件:
import React from 'react';
import {Link} from 'react-router';
import ContactStore from '../stores/ContactStore'
import ContactActions from '../actions/ContactActions';
class Contact extends React.Component {
  constructor(props) {
    super(props);
    this.state = ContactStore.getState();
    this.onChange = this.onChange.bind(this); 
 }
componentDidMount() {
  ContactStore.listen(this.onChange);
  ContactActions.getContact(this.props.params.id);
}
componentWillUnmount() {
  ContactStore.unlisten(this.onChange);
}
componentDidUpdate(prevProps) {
  if (prevProps.params.id !== this.props.params.id) {
    ContactActions.getContact(this.props.params.id);
  }
}
onChange(state) {
  this.setState(state);
}
render() {
  return (
    <div className='container'>
      <div className='list-group'>
        <div className='list-group-item animated fadeIn'>
          <h4>{this.state.contact.displayname}</h4>
          <img src={this.state.imageUrl} />
        </div>
      </div>
    </div>
  );
}
}
export default Contact;