reactjs 平面列表不显示-嵌套平面列表

qyzbxkaa  于 2023-02-22  发布在  React
关注(0)|答案(1)|浏览(113)

我正在尝试嵌套平面列表。我正在使用两个Realm对象数组,需要根据“inventory”数组中的值有条件地显示“ingredients”数组中的项目。
我想知道我是否有我的“返回语句”放置错误或我的逻辑是否是扭曲的。请告知。任何帮助将不胜感激。谢谢。

import * as React from 'react';
import {View, Text, FlatList} from "react-native";
import realm from '../schemas/InventoryDatabase';

export default class ViewInventory extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        FlatListInventoryItems: [],
      };  
      this.state = {
        FlatListIngredientItems: [],
      };
      var inventories = Object.values(realm.objects('Inventories'));
      var ingredients = Object.values(realm.objects('Ingredients'));      
      this.state = {
        FlatListInventoryItems: inventories,
      };
      this.state = {
        FlatListIngredientItems: ingredients,
      };
    }
    ListViewItemSeparator = () => {
      return (
        <View style={{ height: 0.5, width: '100%', backgroundColor: '#000' }} />
      );
    };    
    render() {                                    
      return (
        <View>                
          <FlatList
            data={this.state.FlatListInventoryItems}
            ItemSeparatorComponent={this.ListViewItemSeparator} 
            keyExtractor={(item, index) => index.toString()}           
            renderItem={({ item }) => (                           
              <View style={{ backgroundColor: 'white', padding: 20 }}>
                <Text>Inventory ID: {item.recordID}</Text>
                <Text>Name: {item.inventoryName}</Text>
                <Text>Date: {item.date}</Text>
                <FlatList
                  data={this.state.FlatListIngredientItems}
                  ItemSeparatorComponent={this.ListViewItemSeparator} 
                  keyExtractor={(item2, index) => index.toString()}           
                  renderItem={({ item2 }) => {
                    if (item2.inventoryID == item.recordID) {
                      return (
                        <View style={{ backgroundColor: 'gray', padding: 20 }}>
                          <Text>Ingredient ID: {item2.ingredientID}</Text>
                          <Text>Ingredient Type: {item2.ingredientType}</Text>
                          <Text>Ingredient: {item2.ingredient}</Text>
                        </View>
                      );
                    }
                  }}
                />
              </View>                                  
            )}             
          />
       </View> 
      );
    }
}
bfhwhh0e

bfhwhh0e1#

一切看起来都很好。
但是,ScrollView不应嵌套。请考虑使用map而不是第二个FlatList

相关问题