react-native - React Native FlatList Item onLayout 总是返回 y : 0, x: 0

标签 react-native react-native-flatlist

我想知道 FlatList 中每个项目的值 y,但是,它总是返回 0,是否有任何代码错误? (ScrollView 返回正确的值)

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}
/>

最佳答案

使用 CellRendererComponent 而不是 renderItem 并且您应该能够看到 x、y 被正确填充。

关于react-native - React Native FlatList Item onLayout 总是返回 y : 0, x: 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61559958/

相关文章:

javascript - 为什么 FlatList 在 React Native 中没有动态更新

javascript - React-native:如何根据上一屏幕上选择的数字呈现输入并能够存储输入

react-native - initialScrollIndex 不适用于 FlatList react 原生

android - 在按钮单击时 react native 水平滚动

ios - React-Native:当状态改变时图像不会重新渲染

javascript - 如何从 react-native 中的 GET 请求解析 JSON 数组?

react-native - 我可以在平面列表中使用 ItemSeparatorComponent 中的组件吗?

firebase - 如何在 react-native-firebase 中禁用 recaptcha?

css - React Native 转换原点

css - React Native - FlatList 不滚动