Dataset/JS/ReactBookmarks/image.js (35 lines of code) (raw):

import React, { PropTypes } from 'react'; class Image extends React.Component { constructor(props) { super(props); this.stateFromProps(props); this.handleError = this.handleError.bind(this); } componentWillReceiveProps(newProps) { if (newProps.src !== this.props.src) { this.stateFromProps(newProps); } return newProps === this.props; } stateFromProps(props) { const src = props.src || props.errSrc; this.state = { src, isError: false, }; } handleError(event) { event.preventDefault(); if (this.state.isError) { return; } this.setState({ isError: true, src: this.props.errSrc, }); } render() { return ( <img {...this.props.imgProps} src={this.state.src} onError={this.handleError} /> ); } } Image.propTypes = { src: PropTypes.string.isRequired, errSrc: PropTypes.string.isRequired, imgProps: PropTypes.object, }; export default Image;