React Native - 类型 NSString 无法转换为 ABI41_0_YGValue

问题描述

我正在尝试从 JSON 文件获取数据并计算一个值,我使用 setState 保存了该值,并且我想将其用作 React Native 组件的高度(可触摸的不透明度)。我确实设法获得了这些值,但是当我尝试运行该程序时,会弹出一个错误

//JSON value '85.70' of type Nsstring cannot be converted to a ABI41_0_0YGValue. 
//Did you forget the % or pt suffix? 

我尝试将值放入 styleSheet 中,但没有奏效。我很确定这与值的类型有关。如何将 Nsstring 类型转换为 ABI41_0_0YGValue?以下是我的尝试:

const [value,setValue] = useState(0)
const [isLoading,setLoader] = useState(true)

const fetching = async () => {
    ...//code that fetches the value
    setValue(value)
    setLoader(false)
} 

if (isLoading) {
    return (
        <Text> Loading...</Text>
    )
}

return (
    <View>
        <TouchableOpacity
              style={{height: value,width:30,backgroundColor: "red" }} />
         ... //other parts of the return statement 
    </View>

)

任何帮助将不胜感激!

解决方法

您将值作为字符串传递,只需在保存之前将其转换为数字:

setValue(parseFloat(value))

parseFloat