Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native: VirtualizedList only rendering 10 items

Tags:

react-native

Why does this VirtualizedList only render the first 10 items when it should be rendering 365? If I pass a data variable with more than 10 items it works fine, but fails when I use the getItem method.

export default class ListTest extends Component {
  render() {
    return (
      <View>
        <VirtualizedList
          initialNumberToRender={20}
          windowSize={21}
          getItemCount={(data) => 365}
          getItem={(data, index) => {
            return { key: index };
          }}
          keyExtractor={(item, index) => {
            return item.key;
          }}
          renderItem={({ item, index }) => {
            return (
              <View style={{height:50}}>
                <Text>{item.key}</Text>
              </View>
            );
          }}
        />
      </View>
    );
  }
}
like image 900
mathew Avatar asked Jan 23 '26 09:01

mathew


2 Answers

data needs to be present.

class DataSource {
  getElementAtIndex (index) {
    return { key: index }
  }
}

const data = new DataSource()

function getItem (data, index) {
  return data.getElementAtIndex(index)
}

function getItemCount (data) {
  return 1000
}

const ComponentView = (props) => {
  return (
      <VirtualizedList
        data={data}
        style={{backgroundColor: 'red'}}
        // initialNumToRender={20}
        // maxToRenderPerBatch={}
        // windowSize={21}
        getItemCount={getItemCount}
        getItem={getItem}
        keyExtractor={(item, index) => {
          return item.key
        }}
        renderItem={({ item, index }) => {
          return (
            <View style={{height: 50, backgroundColor: 'yellow'}}>
              <Text>{item.key}</Text>
            </View>
          )
        }}
      />
  )
}
like image 78
Gaston Morixe Avatar answered Jan 25 '26 21:01

Gaston Morixe


You have to still pass a data property when you give it a getItem. getItem is only an accessor to the data variable you pass. By default, getItem is defined as this:

 static defaultProps = {
    disableVirtualization: false,
    getItem: (data: any, index: number) => data[index],
    ...
like image 39
Ramzi C. Avatar answered Jan 25 '26 21:01

Ramzi C.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!