You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
What's the reason that a stateless function cannot return null? #5355
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:
constMeetingMembers=({ members, isLoading, error })=>{if(isLoading){return<iclassName="fa fa-spinner fa-spin"/>;}if(error){return<divclassName="alert alert-danger">{error}</div>;}if(!members){returnnull;// if no members was passed the component should'nt be displayed yet}return<pre>{JSON.stringify(members,null,2)}</pre>;};
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.
Activity
gaearon commentedon Nov 1, 2015
I think this is on the table for 0.15 (#4600):
hnordt commentedon Nov 1, 2015
Awesome. For now I'll return
<span />.Thanks.
syranide commentedon Nov 2, 2015
@hnordt
<noscript />is what React currently uses when you returnnull.sophiebits commentedon Nov 3, 2015
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 ofnull. 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.
andrevinsky commentedon Nov 27, 2015
👍
jimfb commentedon Jan 29, 2016
Fixed in #5884
fix(stateless.component): cannot return null in a stateless function