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

What's the reason that a stateless function cannot return null? #5355

Closed
hnordt opened this issue Nov 1, 2015 · 6 comments
Closed

What's the reason that a stateless function cannot return null? #5355

hnordt opened this issue Nov 1, 2015 · 6 comments
Milestone

Comments

@hnordt
Copy link
Contributor

hnordt commented Nov 1, 2015

When I return null from a stateless function I receive an error message. I would like to know what's the reason that a stateless function cannot return null.

Here is my sample code:

const MeetingMembers = ({ members, isLoading, error }) => {
  if (isLoading) {
    return <i className="fa fa-spinner fa-spin" />;
  }
  if (error) {
    return <div className="alert alert-danger">{error}</div>;
  }
  if (!members) {
    return null; // if no members was passed the component should'nt be displayed yet
  }
  return <pre>{JSON.stringify(members, null, 2)}</pre>;
};

Thank you.

@gaearon
Copy link
Collaborator

gaearon commented Nov 1, 2015

I think this is on the table for 0.15 (#4600):

Remove Support for Inheritless Classes - Enables null return values in plain functions

@hnordt
Copy link
Contributor Author

hnordt commented Nov 1, 2015

Awesome. For now I'll return <span />.

Thanks.

@syranide
Copy link
Contributor

syranide commented Nov 2, 2015

@hnordt <noscript /> is what React currently uses when you return null.

@sophiebits
Copy link
Collaborator

Yeah, this is currently a limitation because React constructs every class (new MeetingMembers()) and if you return a non-object then you'll get the instance of the class back instead of null. We added the requirement that every class extend from React.Component to solve this issue, but 0.14 only deprecates the old behavior and doesn't change it outright. 0.15 will solve this.

Not sure if we have another tracking issue though so we can use this one.

@sebmarkbage sebmarkbage mentioned this issue Nov 3, 2015
7 tasks
@jimfb jimfb added this to the 0.15 milestone Nov 9, 2015
@andrevinsky
Copy link

👍

@jimfb
Copy link
Contributor

jimfb commented Jan 29, 2016

Fixed in #5884

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

6 participants