【问题标题】:FlatList scrollToIndex typescript expoFlatList scrollToIndex 打字稿博览会
【发布时间】:2020-07-21 02:34:22
【问题描述】:

我正在尝试设置滚动以在基于带有 typescript 的变量的 flatList 上建立索引。 我发现的例子是 react 并且似乎不适用于我目前所拥有的。

我的项目实际上有两个平面列表,我希望滚动根据所选内容动态移动。 下面是我所拥有的一个简化示例,只是想知道如何首先使其工作。但显然不行。

    <FlatList

      //tried this, but don't know how to pass the actual index
      ref={(ref) => { ref?.scrollToIndex({index: 0}) }}

      renderItem={({ item }) => {
        return (
          <TouchableOpacity
            onPress={() => onSelect(item, index)}
          > 
           <View>
           </View>
          </TouchableOpacity>
        
      )}}
    />

onSelect 函数,不知道如何将索引传回 flatList

const onSelect = (item, index) => {
  //scrollToIndex(index)?
}

会得到一些指导:)

【问题讨论】:

    标签: typescript react-native expo


    【解决方案1】:

    这样试试

    const onSelect = (item, index) => {
        //scrollToIndex(index)?
        scrollToIndex({animated: true, index});
    }
    

    【讨论】:

    • 嗨卡洛斯,我试过了,但它给了我一个undefined is not an object (evaluating '_this.flatListRef')] 错误。我控制台记录,this,它是未定义的(打字稿,不是本机反应)
    • Cannot find name 'scrollToIndex',这是有道理的,因为onSelect 只是一个函数,不会知道它是从平面列表中调用的
    猜你喜欢
    • 2021-07-04
    • 2021-04-29
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 2021-05-02
    • 1970-01-01
    相关资源
    最近更新 更多