0

I am trying to fetch data from a JSON file and calculate a value, I saved the value using setState and I want to use it as the height of a React Native component (touchable opacity). I did managed to get the values however when I try to run the programme, an error pops up:

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

I tried putting the value into styleSheet and it did not work. I'm pretty sure it is something about the type of the value. How can I convert the type NSString into ABI41_0_0YGValue? Below is my attempt:

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>

)

Any help will be appreciated!

cel-0207
  • 31
  • 7

1 Answers1

0

You are passing the value as a string, just convert it to a number before saving it:

setValue(parseFloat(value))

parseFloat

Auticcat
  • 4,359
  • 2
  • 13
  • 28
  • Thank you so so much!!!! However, do you have any idea why is the value a string even though I fetched the data from JSON file and passed it through some equations..? – cel-0207 Jul 07 '21 at 13:50