12

Have a page that contains a number of images, essentially an image gallery. The gallery page maybe updated to include new or remove existing image entries. An entry for an image is created using the following function, the first time the function is called, it completes successfully, subsequent calls, to handle an updated model, fail with the exception

'Uncaught Error: Invariant Violation'.

Whats causing this?

render: function () {
  var imageEntries = this.props.map(function (entry) {
    var divStyle = {
      backgroundImage: 'url(' + entry.preview + ')',
    };
    return React.DOM.div({key: entry.key, className: 'image-stream-entry'},
      React.DOM.div({className: 'image', style: divStyle}),
        imageMetadata(entry)
      );
   });

  return (
   React.DOM.div(null, imageEntries)
 );
}
4
  • 3
    If you use the unminified build of React you'll get useful error messages. Jan 10, 2014 at 23:16
  • What does imageMetadata do?
    – Ross Allen
    Jan 11, 2014 at 23:26
  • You may need to check isMounted before calling setState if you're doing an async update as the instance of the component may no longer be mounted by the time the request completes. see facebook.github.io/react/docs/component-api.html#ismounted
    – Tom
    Feb 21, 2014 at 11:19
  • 12
    On this line: var imageEntries = this.props.map(function (entry) { I think you're missing the actual prop. It should be something like: var imageEntries = this.props.entries.map(function (entry) {. map being javascript's map function. I had a similar error because of a syntax error.
    – pyrho
    Mar 2, 2014 at 18:36

1 Answer 1

8

It means that the actual DOM is in a different state than the virtual DOM and React is unable to reconcile the two in order to render. Typically this is caused by something ELSE changing the HTML that React is expecting, either a JavaScript error or a different library making changes.

As pyrho pointed out, your call to this.props.map seems to be an issue. this.props should return an object, and Object doesn't have a map method. The Invariate error could be thrown because the component failed to mount into the DOM due to this syntax error.

I wrote up a codepen with a modified version of your code (and some additional supporting code to make the demo work). You can check it out here: http://codepen.io/jhubert/pen/PwqZyr

The core of the code is as follows:

var renderEntry = function (entry) {
  var divStyle = {
    backgroundImage: 'url(' + entry.preview + ')'
  };

  return D.div({ key: entry.key, className: 'image-stream-entry'},
            D.div({className: 'image', style: divStyle}),
            imageMetadata(entry)
         );
};

Gallery = React.createClass({
  displayName: 'Gallery',
  propTypes: {
    entries: React.PropTypes.array.isRequired
  },
  getDefaultProps: function () {
    return { entries: [] };
  },
  render: function () {
    return D.div({ className: 'gallery' }, this.props.entries.map(renderEntry));
  }  
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.