【问题标题】:React Native - type NSString cannot be converted to a ABI41_0_YGValueReact Native - 类型 NSString 不能转换为 ABI41_0_YGValue
【发布时间】:2021-07-07 12:34:27
【问题描述】:

我正在尝试从 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>

)

任何帮助将不胜感激!

【问题讨论】:

    标签: reactjs react-native setstate touchableopacity


    【解决方案1】:

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

    setValue(parseFloat(value))
    

    parseFloat

    【讨论】:

    • 非常感谢!!!!但是,即使我从 JSON 文件中获取数据并通过一些方程式传递它,您是否知道为什么值是字符串?
    猜你喜欢
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多