Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a state object is empty

I have a react js state object and would like to execute some code if the object is empty. Is there something wrong with my logic because the code inside the if block is not getting executed.

if (this.state.errors == null) {
  this.props.updateUser(user);
  this.props.navigation.goBack();
}
like image 206
Nithin Srinivasan Avatar asked Sep 13 '25 15:09

Nithin Srinivasan


1 Answers

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.

like image 95
Ankit Agarwal Avatar answered Sep 15 '25 04:09

Ankit Agarwal