Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught Invariant Violation #56

Closed
123ndy opened this issue Jul 7, 2016 · 5 comments
Closed

Uncaught Invariant Violation #56

123ndy opened this issue Jul 7, 2016 · 5 comments

Comments

@123ndy
Copy link

123ndy commented Jul 7, 2016

This works:

<MediaQuery maxWidth={641}>{parent.content.desktopText.split('\n').map(function(item, index) {
   return (
      <span key={`d83s72h6fd${index}`}>{item}<br/></span>
   )
})}
</MediaQuery>

This creates error:

<MediaQuery minWidth={641}>{parent.content.desktopText.split('\n').map(function(item, index) {
   return (
      <span key={`d83s72h6fd${index}`}>{item}<br/></span>
   )
})}
</MediaQuery>

The only difference is that I use maxWidth in the first and minWidth in the latter.

Any idea why this happens?

Error:
Uncaught Invariant Violation: MediaQuery.render(): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.

@yocontra
Copy link
Owner

yocontra commented Jul 7, 2016

@123ndy React version?

@123ndy
Copy link
Author

123ndy commented Jul 8, 2016

React version 15.2.0

@rhavill
Copy link

rhavill commented Sep 14, 2016

I was getting the same error, but in my case the error was triggered by a MediaQuery component with a maxWidth. I do not know what is causing this, but screen size seems to be a factor. (I was only seeing the error when using a small screen.) In my case, wrapping the contents of MediaQuery with a div tag made the error go away. This might be an acceptable workaround for most people.

I was getting the error when the render function of the MediaQuery component was returning an empty array. The final "else" in the render function returns this.props.children, which was an empty array in this case.

A possible bug fix could be to replace this:

var wrapChildren = React.Children.count(this.props.children) > 1 ||
      typeof this.props.children === 'string' ||
      this.props.children === undefined;

with this:

var wrapChildren = React.Children.count(this.props.children) !== 1 ||
      typeof this.props.children === 'string' ||
      this.props.children === undefined;

(replace "> 1" with "!== 1").

I do not know if that fixes a bug, but it makes the error go away in my case.

@yocontra
Copy link
Owner

@rhavill I think a more permanent fix is to return null when 0 children exist instead of an empty array. Want to send a PR?

rhavill pushed a commit to rhavill/react-responsive that referenced this issue Sep 14, 2016
…nder function when MediaQuery has no children.
yocontra pushed a commit that referenced this issue Sep 15, 2016
Fix for Uncaught Invariant Violation #56.
@yocontra
Copy link
Owner

Closed by #62

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants