小编Joe*_*ler的帖子

react.js替换img src onerror

我有一个react组件,它是列表中的详细信息视图.

如果图像不存在并且存在404错误,我试图用默认图像替换图像.

我通常会在img标签中使用onerror方法,但这似乎不起作用.

我不知道如何做出反应.

这是我的组件:

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>
  );
} …
Run Code Online (Sandbox Code Playgroud)

javascript http-status-code-404 reactjs

39
推荐指数
11
解决办法
4万
查看次数

标签 统计

http-status-code-404 ×1

javascript ×1

reactjs ×1