84757

How do I alternate colors in Flat List (React Native)

Trying to alternate colors in React Natives Flatlist. I believe I need rowID or something similar to do it. This is what I've got so far:

let colors = ['#123456', '#654321', '#fdecba', '#abcdef']; <View > <FlatList style={{backgroundColor: colors[this.index % colors.length]}} data={this.state.dataSource} renderItem={({item}) => <Text style={styles.listStyle}>{item.title}, {item.releaseYear}</Text>} keyExtractor={(item, index) => index} /> </View>

Any ideas?

Answer1:

The renderItem callback argument has a property index that allows you to access the row index for the current row:

<View > <FlatList data={this.state.dataSource} keyExtractor={(item, index) => index} renderItem={({item, index}) => ( <View style={{ backgroundColor: colors[index % colors.length] }}> <Text style={styles.listStyle}>{item.title}, {item.releaseYear}</Text> </View> )} /> </View>

Answer2:

https://www.youtube.com/watch?v=Ca9t95K4Lko&list=PLWBrqglnjNl2yaCcp0HEAWp6zBIfingZ-&index=17

I hope this youtube tutorial would help you get what you need.

Recommend

  • React native SectionList horizontal mode
  • Jquery Autocomplete Search with Ajax Request as sourcedata
  • ScrollToEnd after update data for Flatlist
  • create a unique ref for each element while renderItem() in a SectionList/ FlatList react native
  • Proper way to migrate NDB model property
  • How to debug CUDA kernels in Visual studio or Parallel Nsight
  • If Column Contains String then enter value for that row
  • String not fitting in label which is in a subview for swift
  • UITableViewCell change selectedBackgroundView alpha?
  • UIWebView horizontal bounce not working
  • Change day number color on click - not background
  • Is there a way to disable a specific event in kendo ui scheduler?
  • Custom validator control occupying space even though display set to dynamic
  • Word Open XML Mail Merge
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • jquery mobile loadPage not working
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Unanticipated behavior
  • retrieve vertices with no linked edge in arangodb
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • KeystoneJS: Relationships in Admin UI not updating
  • trying to dynamically update Highchart column chart but series undefined
  • Proper way to use connect-multiparty with express.js?
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • embed rChart in Markdown
  • Change div Background jquery
  • apache spark aggregate function using min value
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • How to push additional view controllers onto NavigationController but keep the TabBar?