Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ReactNative FlatList ScrolltoIndex properly?

Tags:

I'm a newbie in react native and I'm running into an issue I didn't manage to solve by myself.

So far this is how I am using it:

export default class Form extends React.Component {
  state = {index: 0};

  _keyExtractor = (item, index) => item.id;

  myscrollToIndex = () => {
    this.setState({index: ++this.state.index});
    this.flatListRef.scrollToIndex({animated: true,index: this.state.index});
  };

  _renderItem = ({item}) => (
    <View>
      <Question {...item} />
      <Button label='Next' onPress={this.myscrollToIndex} style={styles.buttonNext}/>
    </View>
  )

  render() {
    return (
      <View style={styles.container}>
      <FlatList
        horizontal={false}
        ref={(ref) => { this.flatListRef = ref; }}
        scrollEnabled
        data={this.props.form}
        extraData={this.state}
        keyExtractor={this._keyExtractor}
        renderItem={this._renderItem} />
      </View>
    );
  }
}

I would like to pass the index to myscrolltoindex function but I don't manage to as this.flatListRef gets ruled out when I do that.

Have anyone run into a similar issue ?

Thank you for your time

like image 364
Sydney C. Avatar asked Sep 08 '17 14:09

Sydney C.


1 Answers

Use index param as well. And pass the index around.

For e.g _renderItem = ({item, index}) => ( <View> <Question {...item} /> <Button label='Next' onPress={this.myscrollToIndex(index) } style={styles.buttonNext}/> </View> )

like image 51
Vivek_Neel Avatar answered Oct 11 '22 14:10

Vivek_Neel