npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-get-item-layout-section-list

v0.1.0

Published

Create the getItemLayout prop for a SectionList with React Native

Downloads

8,839

Readme

react-native-get-item-layout-section-list

Create the getItemLayout prop for a SectionList with React Native.

NPM Version

Motivation

The getItemLayout prop in a SectionList is an optimization prop that improves performance of the list by helping it to quickly calculate the size and position of its items.

When you provide the getItemLayout prop, React Native can:

  • Jump directly to any list item without sequentially rendering all previous items.
  • Maintain scroll position accurately during layout changes or content updates.
  • Reduce the need for dynamic measurement as users scroll, leading to smoother experiences.
  • Access other props, such as initialScrollIndex and scrollToLocation

The getItemLayout prop is not trivial to implement for a SectionList. This library provides a simple way to create the getItemLayout prop for a SectionList with fixed or dynamic heights.

Installation

npm install react-native-get-item-layout-section-list

Usage

Basic

This example shows how to use the getItemLayout prop with fixed heights.

import getItemLayout from 'react-native-get-item-layout-section-list';

const ITEM_HEIGHT = 60;
const SECTION_HEADER_HEIGHT = 40;

const buildGetItemLayout = getItemLayout({
  getItemHeight: ITEM_HEIGHT,
  getSectionHeaderHeight: SECTION_HEADER_HEIGHT,
});

<SectionList
  getItemLayout={buildGetItemLayout} 
  keyExtractor={(item, index) => item + index} 
  renderItem={({item}) => (
    <View style={styles.item}>
      <Text style={styles.title}>{item}</Text>
    </View>
  )} 
  renderSectionHeader={({section: {title}}) => (
    <Text style={styles.header}>{title}</Text>
  )} 
  sections={DATA}
/>

Advanced

This example shows how to use the getItemLayout prop with dynamic heights.

import getItemLayout from 'react-native-get-item-layout-section-list';

const SECTION_HEADER_HEIGHT = 40;

const buildGetItemLayout = getItemLayout({
  getItemHeight: (_item, sectionIndex, _itemIndex) => {
    // Return a different height for even and odd items
    return sectionIndex % 2 === 0 ? 60 : 40;
  },
  getSectionHeaderHeight: SECTION_HEADER_HEIGHT,
});

<SectionList
  getItemLayout={buildGetItemLayout}
  keyExtractor={(item, index) => item + index}
  renderItem={({item}) => (
    <View style={styles.item}>
      <Text style={styles.title}>{item}</Text>
    </View>
  )}
  renderSectionHeader={({section: {title}}) => (
    <Text style={styles.header}>{title}</Text>
  )}
  sections={DATA}
/>

Examples

There are fully functional examples in the exmaple directory.

If you'd like to actually run the examples, clone the repository and run the following commands:

cd example
npm install && pod install
npm run ios

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

Releasing

See the releasing guide to learn how to release new versions.

License

This project is licensed under the MIT License - see the LICENSE file for details.