You could check for trimmed string:
{this.props.description.name.trim() == ""}
This trims the string (which removes whitespace and newlines) and then check is if it's an empty string. Here's a CodePen demonstration.
Answer from Andrew Li on Stack OverflowYou could check for trimmed string:
{this.props.description.name.trim() == ""}
This trims the string (which removes whitespace and newlines) and then check is if it's an empty string. Here's a CodePen demonstration.
You can easily to trim the text and compare with empty string. Let try:
{((this.props.description.name.trim() =="") || (this.props.description.name.trim().length==0))
to see how it's work. I've seen you remind about Reducer? You are using Redux, aren't? If you use Redux, for handling form data, you could use Redux Form (https://github.com/erikras/redux-form) to save your time.
Starting with:
return (!value || value == undefined || value == "" || value.length == 0);
Looking at the last condition, if value == "", it's length MUST be 0. Therefore drop it:
return (!value || value == undefined || value == "");
But wait! In JS, an empty string is false. Therefore, drop value == "":
return (!value || value == undefined);
And !undefined is true, so that check isn't needed. So we have:
return (!value);
And we don't need parentheses:
return !value
Q.E.D.
There are just a few revisions I would make.
First, always use === instead of == in Javascript. You can read more about that on Stack Overflow.
Second, since undefined is mutable, I would reccomend using
typeof value === "undefined"
instead of
value === undefined
Third, I would remove the !value and value === "" conditions. They are redundant.
My Revision
I would use a slightly different approach than you:
String.isNullOrEmpty = function(value) {
return !(typeof value === "string" && value.length > 0);
}
This checks if the type of the value is "string" (and thus non-null and not undefined), and if it is not empty. If so, it is not null or empty.
Note that this returns true for non-string inputs, which might not be what you want if you wanted to throw an error for an unexpected input type.
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.
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.
we had an argue about this on our team, I wonder what approach should we consider? what is the better?
We have a user interface like this:
interface User {
name:string;
family?:string;
age:number;family name is optional, so our backend developer sends us this json data for those users that didnt filled the family:
{name: "john",
family:"",
age:18
}and in another object's case they sends null for the optional props...
We have been confused with this not having a solid convention from the backend team! what approach is the better? I think the right way is just removing optional property from the json data instead of setting it to null or "" empty string, I mean just dont add that!
This trick only works with booleans, null, undefined and 0. React tries to render the string even if it is empty.
You can convert the variable to a boolean:
{!!item.mystring &&
<View>
<Text>Should ...</Text>
</View>
}
Or use the ternary operator
{item.mystring ?
<View>
<Text>Should ...</Text>
</View>
:
null
}
This happens with react native as with this, actually it checks the string as a value and gives the error.
return <View>
<Text>Title</Text>>
{item.mystring ?
(<View>
<Text>Should only be rendered when the mystring property exists and has a value</Text>
</View>) : null
}
</View>
You could do something like this to resolve this, or parse it as a boolean first, in your case item.mystring is a string and its inside the render, that too outside text, which makes react-native throw error, it isn't expected, but this has been in react-native since past 2 years :/