sree_iphonedev
sree_iphonedev

Reputation: 3534

Customising height for item on a section in SectionList

As part of learning react-native, I am experimenting various feature as in native iOS (TableView or CollectionView) or android app (RecycleView). I have implemented a sample screen with SectionList and FlatList and now I wanted to provide different height for item or section in a SectionList.

Referring various blogs, I have seen a package which is available in GitHub 'https://github.com/jsoendermann/rn-section-list-get-item-layout'. Could anyone please suggest me what is the standard way to achieve such features? It seems the package is last updated 2 years back, so I don't prefer it and looking for the standard solution. Thanks in advance.

Upvotes: 0

Views: 2007

Answers (1)

Gaurav Roy
Gaurav Roy

Reputation: 12235

This is same as that of in section list or if you want to use Flat list, inside that you provide a prop called renderItem and inside that you provide your custom comopnent. So as you do styling for any custom component, its the same. check below , inside function Item ive added styles.item as the styling and added a height of 80 there, you can play around. Check the link exmaple :

*import React from 'react';
import {
  StyleSheet,
  Text,
  View,
  SafeAreaView,
  SectionList,
} from 'react-native';
import Constants from 'expo-constants';

const DATA = [
  {
    title: 'Main dishes',
    data: ['Pizza', 'Burger', 'Risotto'],
  },
  {
    title: 'Sides',
    data: ['French Fries', 'Onion Rings', 'Fried Shrimps'],
  },
  {
    title: 'Drinks',
    data: ['Water', 'Coke', 'Beer'],
  },
  {
    title: 'Desserts',
    data: ['Cheese Cake', 'Ice Cream'],
  },
];

function Item({ title }) {
  return (
    <View style={styles.item}>
      <Text style={styles.title}>{title}</Text>
    </View>
  );
}

export default function App() {
  return (
    <SafeAreaView style={styles.container}>
      <SectionList
        sections={DATA}
        keyExtractor={(item, index) => item + index}
        renderItem={({ item }) => <Item title={item} />}
        renderSectionHeader={({ section: { title } }) => (
          <Text style={styles.header}>{title}</Text>
        )}
      />
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop: Constants.statusBarHeight,
    marginHorizontal: 16,
  },
  item: {
    backgroundColor: '#f9c2ff',
    padding: 20,
    marginVertical: 8,
    height:80
  },
  header: {
    fontSize: 32,
    margin:50
  },
  title: {
    fontSize: 24,
  },
});*

hope it helps.

Upvotes: 1

Related Questions