React-native: FlatList scrollToIndex() dont work without specifying the getItemLayout prop

Created on 21 Jul 2017  路  2Comments  路  Source: facebook/react-native

React native version 0.46.4

quickmemo _2017-07-20-15-17-23

`
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
FlatList,
View
} from 'react-native';

export default class FlatListError extends Component {
constructor(props){
super(props);
this.state = {
data: [],
}
}
componentDidMount(){
let dataL = [];
for (var i = 0; i < 100; i++) {
let obj = { key: Element ${i}, content: 'Nullam ac dictum elit. Morbi sollicitudin dignissim dolor at egestas. Fusce dignissim metus in augue blandit suscipit. Nam dapibus nulla' };
dataL.push(obj);
}
this.setState({data: dataL});
}
_scrollToIndex = () => {
/*
HOW FIX THIS BUG
set initialNumToRender = data.length
and max scrollToIndex initialNumToRender -2

*/

console.warn(this.state.data.length); //100
let index = 100;
if(index > this.state.data.length -2){
  this.flatlist.scrollToEnd({animated: true});
}else{
  this.flatlist.scrollToIndex({animated:true , index: this.state.data.length -2, viewPosition: 0}) // Go to element 98 
}

}
_renderList = ({item}) => (



);
render() {
return (

Welcome to React Native!

ref={(ref) => this.flatlist = ref}
onLayout={() => this._scrollToIndex()}
initialNumToRender={this.state.data.length}
data={this.state.data}
renderItem={this._renderList}
/>

  </View>
);

}
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
flatContainer:{
flex:1,
width:'100%',
alignItems:'center',
}
});

AppRegistry.registerComponent('FlatListError', () => FlatListError);

`
@hramos Gracias por la pronta respuesta

HOW FIX THIS BUG
set initialNumToRender = data.length
and max scrollToIndex initialNumToRender -2 || max scrollToIndex data length -2

Locked

All 2 comments

If the error continues use the function setTimeout whit 100 ms. e.g
setTimeout(() => { this.flatlist.scrollToIndex({animated:true , index: i, viewPosition: 0.5}) }, 100);

Hey, thanks for reporting this issue!

It looks like your description is missing some necessary information, or the list of reproduction steps is not complete. Can you please add all the details specified in the template? This is necessary for people to be able to understand and reproduce the issue being reported.

I am going to close this, but feel free to open a new issue with the additional information provided. Thanks!

Was this page helpful?
0 / 5 - 0 ratings