Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="//fb.me/react-0.14.3.js"></script>
<script src="//fb.me/react-dom-0.14.3.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div id="container"></div>
</body>
</html>
 
.image { opacity: 0; transition: opacity 1.0s; }
.image-loaded { opacity: 1; }
 
var ImageComponent = React.createClass({
  getInitialState: function() {
    return {
      loaded: false,
    };
  },
  onImageLoad: function() {
    if (this.isMounted()) {
      this.setState({ loaded: true });
    }
  },
  componentDidMount: function() {
    var imgTag = ReactDOM.findDOMNode(this.refs.img);
    var imgSrc = imgTag.getAttribute('src');
    var img = new window.Image();
    img.onload = this.onImageLoad;
    img.src = imgSrc;
  },
  render: function() {
    var { className, ...props } = this.props;
    var rootClassName = className ? className + ' image' : 'image';
    if (this.state.loaded) {
      rootClassName += ' image-loaded';
    }
    return <img ref="img" {...props} className={rootClassName} />;
  }
});
ReactDOM.render(
  <ImageComponent src="http://voices.nationalgeographic.com/files/2013/04/NationalGeographic_1467467.jpg" width="500" />,
  document.getElementById('container')
);
Output

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers