javascript - How to use ReactNative FlatList ScrolltoIndex properly? - Stack Overflow

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

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'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

Share Improve this question edited Apr 3, 2018 at 6:32 emtiajium 8110 bronze badges asked Sep 8, 2017 at 14:17 Sydney C.Sydney C. 97811 silver badges23 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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> )

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744388576a4571791.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信