Given that this.state.errors is an object you can do this,
//when this.state.errors object is empty
if (Object.keys(this.state.errors).length === 0) {
this.props.updateUser(user);
this.props.navigation.goBack();
}
Object.keys will return an array or all the keys from the object this.state.errors. Then you can check the length of that array to determine if it is an empty object or not.
Given that this.state.errors is an object you can do this,
//when this.state.errors object is empty
if (Object.keys(this.state.errors).length === 0) {
this.props.updateUser(user);
this.props.navigation.goBack();
}
Object.keys will return an array or all the keys from the object this.state.errors. Then you can check the length of that array to determine if it is an empty object or not.
Actually, You need to first check this.state.errors is exist or not and then for object is null or not
if (this.state.errors && !Object.keys(this.state.errors)) {
this.props.updateUser(user);
this.props.navigation.goBack();
}
Is object empty? - javascript
Checking if a state object is empty
Check if react element is empty - javascript
React Component check is variable is empty
What is the fastest way to check if an object is empty in React?
Are there any performance considerations when using JSON.stringify() to check if an object is empty?
Why should I use Lodash's isEmpty method in my React project?
Videos
For ECMAScript5 (not supported in all browsers yet though), you can use:
Object.keys(obj).length === 0
I'm assuming that by empty you mean "has no properties of its own".
// Speed up calls to hasOwnProperty
var hasOwnProperty = Object.prototype.hasOwnProperty;
function isEmpty(obj) {
// null and undefined are "empty"
if (obj == null) return true;
// Assume if it has a length property with a non-zero value
// that that property is correct.
if (obj.length > 0) return false;
if (obj.length === 0) return true;
// If it isn't an object at this point
// it is empty, but it can't be anything *but* empty
// Is it empty? Depends on your application.
if (typeof obj !== "object") return true;
// Otherwise, does it have any properties of its own?
// Note that this doesn't handle
// toString and valueOf enumeration bugs in IE < 9
for (var key in obj) {
if (hasOwnProperty.call(obj, key)) return false;
}
return true;
}
Examples:
isEmpty(""), // true
isEmpty(33), // true (arguably could be a TypeError)
isEmpty([]), // true
isEmpty({}), // true
isEmpty({length: 0, custom_property: []}), // true
isEmpty("Hello"), // false
isEmpty([1,2,3]), // false
isEmpty({test: 1}), // false
isEmpty({length: 3, custom_property: [1,2,3]}) // false
If you only need to handle ECMAScript5 browsers, you can use Object.getOwnPropertyNames instead of the hasOwnProperty loop:
if (Object.getOwnPropertyNames(obj).length > 0) return false;
This will ensure that even if the object only has non-enumerable properties isEmpty will still give you the correct results.
Given that this.state.errors is an object you can do this,
//when this.state.errors object is empty
if (Object.keys(this.state.errors).length === 0) {
this.props.updateUser(user);
this.props.navigation.goBack();
}
Object.keys will return an array or all the keys from the object this.state.errors. Then you can check the length of that array to determine if it is an empty object or not.
Actually, You need to first check this.state.errors is exist or not and then for object is null or not
if (this.state.errors && !Object.keys(this.state.errors)) {
this.props.updateUser(user);
this.props.navigation.goBack();
}
render() {
return (
<div>
<div>{!!(this.myvar)?this.myvar:"whatever you want"}</div>//
</div>
);
}
!!: check for undefined, null, and empty value
The provided solution runs afoul of eslint's no-extra-boolean-casts rule.
An alternate method that makes eslint happy would look like this:
render() {
return (
<div>
<div>{!this.myvar ? "whatever you want" : this.myvar}</div>//
</div>
);
}