How to set different color for every item in FlatList in React Native

 See the below example to set different colors for every item in a FlatList.

Example:-

import React from 'react';
import { ViewStyleSheet , TextScrollViewfrom 'react-native';

const DATA=[
  'Item1',
  'Item2',
  'Item3',
  'Item4',
  'Item5',
  'Item6',
  'Item7',
  'Item8',
  'Item9',
  'Item10',
  'Item11',
  'Item12',
  'Item13',
  'Item14',
  'Item15',
]

function getColor() {
var ColorCode = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
return ColorCode;
}

export default function Example() {
  return (
    <View style={styles.conatainer}>
      <ScrollView>
      {
        DATA.map((itemindex)=>{
          return(
            <View style={[styles.view1,{backgroundColor:getColor()}]} key={index}>
              <Text style={styles.text}>{item}</Text>
            </View>
          )
        })
      }
      </ScrollView>
    </View>
  );
}

const styles = StyleSheet.create({
  conatainer: {
    flex: 1,
  },
  view1:{
    //backgroundColor:'pink',
    alignItems:'center',
    justifyContent:'center',
    height:100
  },
  text:{
    fontSize:30,
    fontWeight:'bold',
    color:'white'
  }
})

OUTPUT:-







Comments

Popular posts from this blog

Button in React-Native

StyleSheet in React Native

State in React Native