Thursday, May 23, 2024
64
rated 0 times [  65] [ 1]  / answers: 1 / hits: 103476  / 6 Years ago, sat, january 20, 2018, 12:00:00

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();
}

More From » react-native

 Answers
20

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.


[#55415] Tuesday, January 16, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gregorio

Total Points: 362
Total Questions: 95
Total Answers: 93

Location: Puerto Rico
Member since Sun, Jun 27, 2021
3 Years ago
gregorio questions
Fri, Apr 8, 22, 00:00, 2 Years ago
Mon, Sep 6, 21, 00:00, 3 Years ago
Sun, Sep 13, 20, 00:00, 4 Years ago
;