【问题标题】:how can i use scrollToIndex in FlatList React native?我如何在 FlatList React native 中使用 scrollToIndex?
【发布时间】:2021-03-16 22:49:35
【问题描述】:

我想要做的是,当我运行 onPress 时,我想向下滚动到 y 的值(即 499)

但如果我使用我的代码,它什么都不起作用

这是我的代码

    const TodoList = ({}) => {

      const height = Dimensions.get('window').height;
      const flatListRef = useRef()

    
      return (
        <FlatList
        ref={flatListRef}
          style={{height}}
          keyExtractor={(item) => String(item.id)}

          renderItem={({item}) => (
            <TodoItem
            onPress={() => {
              const node = flatListRef.current;
              if (node) {
                node.scrollToIndex({x: 0, y: 499, animated: true, index:0});
              }
            }}
            />
          )}
        />

我应该修复什么代码?...

【问题讨论】:

    标签: javascript node.js reactjs react-native


    【解决方案1】:

    我认为您使用了错误的方法。你应该使用scrollToOffset

    if (node) {
        node.scrollToOffset({ offset: 499, animated: true });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-17
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      • 2020-01-25
      • 2020-12-28
      相关资源
      最近更新 更多