【问题标题】:Why does FlatList's renderItem prop return inconsistent data from array?为什么 FlatList 的 renderItem 属性从数组中返回不一致的数据?
【发布时间】:2020-01-05 08:36:33
【问题描述】:

目标:使用 react-native 的 FlatList 来呈现可触摸图像的列表,当按下时,显示相应图像的 Modal。

List.js:

import React, { useState } from 'react';
import {
    View,
    Text,
    Image,
    StyleSheet,
    Modal,
    FlatList,
    Dimensions,
    TouchableOpacity,
    TouchableWithoutFeedback
} from 'react-native';
const { height, width } = Dimensions.get('window');

const List = (props) => {
    const [ visible, setVisible ] = useState(false);
    return (
        <View style={styles.container}>
            <FlatList
                data={props.data}
                numColumns={2}
                renderItem={({ item, index }) => {
                    return (
                        <View>
                            <TouchableOpacity onPress={() => setVisible(true)} style={styles.itemContainer}>
                                <Image style={styles.img} resizeMode={'contain'} source={item.img} />
                                <View style={styles.details}>
                                    <Text style={{ color: 'white', fontWeight: 'bold' }}>{item.name}</Text>
                                    <Text style={{ color: 'white' }}>{item.date}</Text>
                                </View>
                            </TouchableOpacity>
                            <Modal
                                animationType="fade"
                                transparent={false}
                                visible={visible}
                                presentationStyle={'overFullScreen'}
                            >
                                <View style={styles.modalContainer}>
                                    <TouchableWithoutFeedback onPress={() => setVisible(false)}>
                                        <Image resizeMode={'contain'} style={styles.modalImg} source={item.img} />
                                    </TouchableWithoutFeedback>
                                </View>
                            </Modal>
                        </View>
                    );
                }}
            />
        </View>
    );
};

const styles = StyleSheet.create({
    container: {
        flex: 1,
        alignItems: 'center'
    },
    img: {
        height: width * 0.65,
        width: width * 0.45
    },
    modalImg: {
        left: width * 0.12,
        top: height * 0.2
    },
    modalContainer: {
        flex: 1,
        backgroundColor: 'rgba(69, 74, 102, .7)'
    },
    details: {
        position: 'absolute',
        backgroundColor: 'rgba(137,137,137, 0.75)',
        borderRadius: 10,
        height: width * 0.125,
        width: width * 0.4,
        left: '4%',
        top: '75%',
        justifyContent: 'center',
        alignItems: 'center'
    }
});

export default List;

实施:

import React from 'react';
import { StyleSheet, Dimensions, SafeAreaView } from 'react-native';
import Component from './Component';

const { width } = Dimensions.get('window');

const testArr = [
    { name: 'Josh Gordon', date: 'November 7, 2019', img: require('./src/assets/img/josh.png') },
    { name: 'Kylie Jenner', date: 'December 21 2019', img: require('./src/assets/img/kylie.png') },
    { name: 'Logic', date: 'December 15, 2019', img: require('./src/assets/img/logic.png') }
];

const App = () => {
    return (
        <SafeAreaView style={styles.screen}>
            <Component data={testArr} />
        </SafeAreaView>
    );
};

const styles = StyleSheet.create({
    screen: {
        flex: 1,
        backgroundColor: '#E6E6E6'
    }
});

export default App;

问题:最初的 FlatList 使用所有三个不同的图像进行渲染,但是当按下 任何 个图像按钮时,模型仅显示 最后一个图像数据数组,即使使用了相同的item.img 变量...为什么?

【问题讨论】:

    标签: react-native react-native-flatlist


    【解决方案1】:

    您的模式与列表中的项目一样多,但只有一个visible 变量供所有模式使用。当您单击任何项​​目时,visible 对所有模态都变为 true,并且它们会同时呈现,因此您只能看到其中一个。

    不要将模态框放在 FlatList renderItem 中。模态应该始终在 dom 中呈现(但不总是 visible)。只制作一个模态,并在状态中存储相关信息(模态可见性以及要在模态中显示的项目)。然后在你的 TouchableOpacity 的 onPress 中改变它

    【讨论】:

    • 当我尝试将item.img(我认为是文件路径作为字符串)保存到状态,然后尝试在模式中渲染该图像时,什么也没有发生。然后我发现item.img 只是一个数字。不知何故,路径更改为一个数字,所以我不能使用我保存的状态作为图像源在模态中......有什么想法吗?
    • 在你require() 一些资源之后,在运行时所需的路径不存在。它仅在您的计算机上的构建期间存在。图像被打包到应用程序中,并由资源 id(您正在谈论的数字)引用。因此,如果您需要某些东西并打印您拥有的东西,那么您总是会得到数字。不要费心将这个数字存储在状态中,而是在数组中存储条目的标识符,例如,您为其显示模态的人的姓名,或者他们在数组中的索引。然后,您将能够通过该名称或索引从您的 testArr 获取图像
    • 从 props 访问 testArr 中的 img 会得到一个数字。将modal和onpress函数移动到父组件直接访问testArr也会产生一个数字
    • 那我怎么显示图片
    • 像往常一样,将require 的结果提供给sourceImage 属性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多