我有一个react js state对象,如果该对象为空,我想执行一些代码.我的逻辑是否有问题,因为if块中的代码没有被执行.
if (this.state.errors == null) {
this.props.updateUser(user);
this.props.navigation.goBack();
}
我有一个react js state对象,如果该对象为空,我想执行一些代码.我的逻辑是否有问题,因为if块中的代码没有被执行.
if (this.state.errors == null) {
this.props.updateUser(user);
this.props.navigation.goBack();
}
考虑到this.state.errors
是一个物体,你可以这样做,
//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
将从对象this.state.errors
返回一个数组或所有键.然后可以判断该数组的长度,以确定它是否为空对象.