React Native onLayout nativeEvent getHeight Error

Viewed 17

When run this code I get error:

JSON value "of type NSString cannot be converted to a ABI46_0_0YGValue. Did you forger the % or pt suffix?

But why? How should change the code to stop the error?

import { Text, View} from 'react-native';
import {useState} from 'react'

export default function App() {
  
  const [height, setHeight] = useState('')
  
return (
    <View style={{
        backgroundColor: 'green',
        flex: 1
    }}>

    <View style={{
        backgroundColor: 'blue',
        position: 'absolute',
        left: 0,
        right: 0,
        top: 0,
        bottom: height,
    }}>
    </View>

<View
onLayout={({ nativeEvent }) => {
const { x, y, width, height } = nativeEvent.layout
setHeight(height)
}}

style={{
 backgroundColor: 'red',
 position: 'absolute',
 left: 0,
 right: 0,
 bottom: 0,
 padding: 20
}}>
<Text>Lorem Ipsum</Text>
</View>

</View>
);
}


1 Answers

You're using an empty string as the default value for your height, here: const [height, setHeight] = useState(''). Try setting it to a number instead: const [height, setHeight] = useState(0)

Before: const [height, setHeight] = useState('')

After: const [height, setHeight] = useState(0)

Αnswered by Kapobajza

Related