What is the best way to organize a flatlist for rendering?

I'm struggling with separating some flat-lists into different components. How can I arrange the rendering of the flat-list like the sample form (Picture "Sample UI")? I've tried, but it's not working correctly as it renders flat list A first and then flat list B. Please help me solve this issue. Is there another way to approach this problem? Thank you.

Here is my code:

Header component


import React, {Component} from 'react'
import {View,Text,StyleSheet,FlatList,Image} from 'react-native'
import data from '../data/FlatListData.json'

// Component to render flat-list showing artist names and albums.
export default class Header extends Component{
  renderItem(item) {
    return(
      <View style = {styles.flatlist}>
        <Image
          source = {{uri: item.item.thumbnail_image}} style ={styles.imageThumbnail}>
        </Image>
        <View style= {{
                        flex: 1,
                        flexDirection: 'column'
                      }}>
            // Show title
            <Text style = {styles.textTitle}>{item.item.title}</Text>  
            // Show name of artist
            <Text style = {styles.textArtist}>{item.item.artist}</Text> 
        </View>
      </View>
    );
  }

  render(){
      return(
        <View style = {{flex: 1}}>
          <View style = {{flex: 1}}>
              <FlatList
                data = {data}
                renderItem = {this.renderItem}
                keyExtractor = { (item) => item.title.toString() }
              />
          </View>
        </View>
      );
    }
}


// Styles
const styles = StyleSheet.create({
  container: {
    marginTop: 20,
    justifyContent: 'center',
    alignItems: 'center',
    borderColor: '#d6d7da',
    borderRadius: 4,
    borderWidth: 0.5,
    fontSize: 1,
    height: 50,
    marginLeft: 10,
    marginRight:10
  },
  text: {
    fontSize: 20,
  },
  flatlist: {
    flex: 1,
    flexDirection: 'row',
    marginRight: 20,
    marginLeft: 20,
    alignItems: 'center',
    justifyContent: 'center',
    borderRadius: 4,
    borderWidth: 0.4,
    borderColor: '#d6d7da',
    marginTop: 20,
    height: 60
  },
  imageThumbnail: {
    width: 50,
    height: 50,
    margin: 5
  },
  image: {
    width: 320,
    height: 240,
    margin: 5
  },
})

Image Component


import React, {Component} from 'react'
import {View,Text,StyleSheet,FlatList,Image} from 'react-native'
import data from '../data/FlatListData.json'

// Component to show images of albums
export default class ImageCom extends Component{
  renderItem(item) {
    return(
      <View style = {styles.flatlist}>
        // Image of albums
        <Image
          source = {{uri: item.item.image}} style ={styles.image}>
        </Image>
      </View>
    );
  }

  render(){
      return(
        <View style = {{flex: 1}}>
          <View style = {{flex: 1}}>
              <FlatList
                data = {data}
                renderItem = {this.renderItem}
                keyExtractor = { (item) => item.title.toString() }
              />
          </View>
        </View>
      );
    }
}


// Style sheet
const styles = StyleSheet.create({
  flatlist: {
    flex: 1,
    flexDirection: 'row',
    marginRight: 20,
    marginLeft: 20,
    alignItems: 'center',
    justifyContent: 'center',
    borderRadius: 4,
    borderWidth: 0.4,
    borderColor: '#d6d7da',
    marginTop: 5,
    height: 250
  },
  image: {
    width: 320,
    height: 240,
    margin: 5
  },
})

App.js


import React, {Component} from 'react'
import {View,Text,StyleSheet,FlatList} from 'react-native'
import Tabbar from './src/components/Tabbar'
import Header from './src/components/Header'
import ImageCom from './src/components/ImageCom'

// Combine components and display them on the UI
export default class App extends Component{
  render() {
    return(
      <View style = {styles.container}>
        <Tabbar />   // Hello
        <Header />    // Artist and albums
        <ImageCom />   // Images of albums
      </View>
    );
  }
}

const styles = StyleSheet.create ({
  container: {
    flex: 1,
  },
});

Answer №1

Hello, @angelos_lex mentioned that you don't need two flatlists, so I believe you can display the image component in the header component only.

export default class Header extends Component{
    renderItem(item) {
        return(
          <View style = {{flexDirection:'colomn'}}> **//give require styles**
            <View style = {styles.flatlist}>
                <Image
                  source = {{uri: item.item.thumbnail_image}} style = {styles.imageThumbnail}>
                </Image>
                <View style= {{ flex: 1,flexDirection: 'column' }}>
                 //show title
                   <Text style = {styles.textTitle}>{item.item.title}</Text>  
                 //show name of artist
                   <Text style = {styles.textArtist}>{item.item.artist}</Text> 
                </View>
            </View>
            <Image source = {{uri: item.item.image}} style ={styles.image}/> **// i am not sure what uri should be given**
         </View>
     );

 **// remaining code**

Answer №2

Is it necessary to use two distinct flatlists for this particular scenario? It seems that a single Flatlist would suffice if you prefer the order: header_No1 -> image_No1 -> footer_No1 followed by header_No2 -> image_No2 -> footer_No2. This way, all components such as "header," "image," and "footer" can be rendered within one Flatlist.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Incorporating a radio button input into the parent component in a React application

The main component stores all the state data. Whenever there is a change in any input field, the function myChangeHandler is triggered and updates the state accordingly. myChangeHandler(event) { this.setState({ [event.target.name]: event.target.value }) ...

Obtain the complete path in Vue router by utilizing nested routes

After creating nested routes for Vue Router, I encountered a problem while using the routes to generate a navigation menu. Currently, I am using route.path in 'router-link :to=' which only gives me a part of the path. I want to include the absolu ...

What is the best method for integrating opensea-js using a script tag in a vanilla HTML/JS environment?

Is there a way to incorporate opensea-js into an html/js project that does not rely on node.js? The source code for opensea-js is only available on github and npm at https://github.com/ProjectOpenSea/opensea-js I came across this link: However, when I tr ...

Preventing the insertion of a line break when using Shift + Enter in Vuejs

Whenever I use a textarea to enter text, I find that I have to press Shift + Enter every time to send the text. However, upon sending, it adds /n at the end. I prefer using the Enter key for newline instead of submitting the form. Example: hello => ...

Issue encountered with Maven and Selendroid: Unable to find specified archiver for 'apk' files

Currently, I am in the process of creating a test case that utilizes selendroid-standalone for managing the connection to a mobile phone. The project is structured with Maven and includes multiple submodules. To incorporate this functionality, I have inclu ...

A guide on determining the return type of an overloaded function in TypeScript

Scenario Here is a ts file where I am attempting to include the type annotation GetTokenResponse to the function getToken. import { ConfigService } from '@nestjs/config'; import { google, GoogleApis } from 'googleapis'; import { AppCon ...

"Adjusting the width of columns in a Datatable

I have arranged a data table with multiple rows and columns, and I am seeking guidance on how to increase the width of the "Tel. 1, Tel. 2, and Fecha" columns to ensure that the text appears on a single line. I've attempted adjusting the s width and t ...

How can the Angular Js framework be utilized to create a pop-up feature in combination with Bootstrap?

I'm currently working on a website using AngularJS and Bootstrap. When the site is loading, I make a server call to fetch some data. This process takes some time, and during this interval, I want to display a pop-up message indicating that the user s ...

The UIViewController in question is unable to display a UIAlertController

After a user clicks the submit button in my application, the SubmitViewController appears on top of the active view controller to show upload progress. In the event of an error notification, an Alert should pop up with the specific error message and then ...

Using Vue to pass an array of rules to a child component

Currently, I am passing a set of props called "propSet" to a child component. These props are computed and detect an 'edit mode' boolean that changes accordingly. The "propSet" defines the following form input props: color, filled, dense, outlin ...

Type of Data for Material UI's Selection Component

In my code, I am utilizing Material UI's Select component, which functions as a drop-down menu. Here is an example of how I am using it: const [criteria, setCriteria] = useState(''); ... let ShowUsers = () => { console.log('Wor ...

Running JavaScript code when the route changes in Angular 6

Currently, I am in the process of upgrading a website that was originally developed using vanilla JS and JQuery to a new UI built with Angular and typescript. Our site also utilizes piwik for monitoring client activity, and the piwik module was created i ...

Pause before proceeding to the next iteration in the loop

I am currently facing an issue while trying to send a message through an API using a function. The function returns a value called messageLogId, which I'm attempting to update in the main loop at Attendence. However, when executing the code, the value ...

Send back Complex data type to display using AJAX

My Objective I am dealing with a complex type generated by EF from a stored procedure. This type returns a list of GetAvailableRooms_Results. I want the user to choose two dates and then get a list of available rooms (complex type) returned by the stored ...

retrieving data from a php file using ajax for a post request

Utilizing ajax, I have invoked the page search.php: function search(){ var title=$("#search").val(); if(title!=""){ $.ajax({ type:"post", url:"sear ...

Generating a collection of items within a JavaScript object

Struggling with sending a json object to a java API backend that requires an Object containing a list of objects. Wondering if it's possible to create a list of objects inside a javascript Object. While I know we can create an "Array" of objects with ...

The method for retrieving and entering a user's phone number into a text field upon their visit

Objective: To enhance the user experience by automatically filling in the phone number input field when a mobile user views the page, making it easier to convert them into leads. A privacy policy will, of course, be in place. This page will offer a promo ...

Update the HTML weather icon with data from JSON

I have a collection of weather icons stored on my computer. How can I customize the default weather icons with my own set of icons? In the JSON file, there is a variable like this: "icon":"partlycloudy" <html> <head> <script src="http://c ...

Minifying Angular using grunt leads to 'Error initializing module' issue

Currently, I have multiple controllers, models, and services set up as individual files for preparation before minification. My goal is to combine and minify all these files into one JS file for production. To illustrate how my files are structured, here ...

Transitioning from the login screen to the main page in Ionic with the help of Angular

My code is running smoothly, but I encountered an issue when clicking the login button. It changes the state as depicted in the image, altering the URL but failing to open the subsequent page. Can anyone provide guidance on how to redirect to the next page ...