Invariant Violation:[1063,{"width":"<<NaN>>"}] 之NaN相关错误

项目中我们经常会自定义一些组件,然后传递数据进来。偶尔我们会拿到传递进来的数据进行一些运算,然后问题来了,

在某些偶发情况下,就会碰到,{"width":"<<NaN>>"}之类的错误

定义的数据接口(都是必传字段)

export interface IProps extends IBasePageProp {
    successorRatio: number,//网络数据
     number,//本地数据
}
 <View style={{ height: 8, borderRadius: 4, backgroundColor: this.showColor(),  this.props.width  * ((this.props.successorRatio?this.props.successorRatio:0) * 0.01) }}></View>

发现successorRatio(网络数据直接使用的时候,不需要判空处理。

如果参与运算,有些接口需要判空,有些接口又不需要判空,保险起见,就是都做判空处理。

而width(本地数据)不论哪种情况都不要判空处理

综上所述:

一般我们遇到{"width":"<<NaN>>"}之类的错误,

1、在使用的地方做判空处理(来个三目即可)

2、在传入的地方做判空处理

原文地址:https://www.cnblogs.com/lijianyi/p/14291755.html