Search code examples
reactjsrealmnativeflatlist

Flatlist Does Not Appear - Nested Flatlists


I am attempting to nest a Flatlist. I am using two Realm object arrays and need to conditionally display items from the "ingredients" array based on a value within the "inventories" array.

I am wondering if I have my "return statements" placed incorrectly or whether my logic is skewed. Please advise. Any help would be much appreciated. Thank you.

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> 
      );
    }
}


Solution

  • Everything looks ok.

    However, ScrollViews should never be nested. Consider using map instead of your second FlatList.