Reputation: 355
In react js if you we return empty string "" instead of null.
What is the difference ?
Suppose below is the component.
const Abc = () => {
return ''
}
Upvotes: 5
Views: 11279
Reputation: 355
Whenever you return an empty string instead of null from a component, react will create a text node for that response.
Edit: returning null won't create an additional text node
You can check it here.
Upvotes: 6
Reputation: 582
Returning null is usually the best idea if you intend to indicate that no data is available.
An empty object implies data has been returned, whereas returning null clearly indicates that nothing has been returned.
Additionally, returning a null will result in a null exception if you attempt to access members in the object, which can be useful for highlighting buggy code - attempting to access a member of nothing makes no sense. Accessing members of an empty object will not fail meaning bugs can go undiscovered.
Personally, I like to return empty strings for functions that return strings to minimize the amount of error handling that needs to be put in place.
Upvotes: 3