Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native FlatList Item onLayout always return y: 0, x: 0

I would like know the value y in every item in a FlatList, however, it always to return 0, is any code wrong? (ScrollView returns the correct value)

function getLayout(event, id) { 
    const { y } = event.nativeEvent.layout;
    console.log("y: " + y + "| id: " + id);
    console.log(event.nativeEvent);    
}

function Item({ id, img }) {
    return (
        <View style={styles.imageBoxContainer} onLayout={(event) => getLayout(event, id)} >
            <Image source={img} />          
        </View>
    );
}  

<FlatList
    data={DATA}
    renderItem={({ item }) => <Item img={item.img} id={item.id}/>}
    keyExtractor={item => item.id}
/>
like image 348
Celia Avatar asked Jan 30 '26 17:01

Celia


1 Answers

use CellRendererComponent instead of renderItem and you should be able to see x, y getting populated correctly.

like image 141
gaurav107 Avatar answered Feb 02 '26 10:02

gaurav107