react-native - 任何状态更改时的 FlatList ScrollView 错误 - 不变违规 : Changing onViewableItemsChanged on the fly is not supported

标签 react-native react-native-flatlist

onViewableItemsChanged 当应用程序中的状态发生变化时,它似乎不起作用。这样对吗?
如果是这种情况,似乎它不会很有用....
否则,用户将被迫向我们onScroll为了确定位置或类似的东西......
重现步骤

  • 请引用snack
  • Repo 也已上传到 github
  • 使用 onViewableItemsChanged 时,任何状态更改都会产生错误
  • 这个错误甚至意味着什么?

  • 注意:放置 onViewableItemsChanged const 中的函数在渲染方法之外也无济于事...
    <FlatList
        data={this.state.cardData}
        horizontal={true}
        pagingEnabled={true}
        showsHorizontalScrollIndicator={false}
        onViewableItemsChanged={(info) =>console.log(info)}
        viewabilityConfig={{viewAreaCoveragePercentThreshold: 50}}
        renderItem={({item}) =>
            <View style={{width: width, borderColor: 'white', borderWidth: 20,}}>
                <Text>Dogs and Cats</Text>
            </View>
        }
    />
    
    实际行为
    错误
    image

    最佳答案

    基于@woodpav 评论。使用功能组件和 Hook。
    分配两个 viewabilityConfigonViewableItemsChanged引用并使用这些。像下面这样:

      const onViewRef = React.useRef((viewableItems)=> {
          console.log(viewableItems)
          // Use viewable items in state or as intended
      })
      const viewConfigRef = React.useRef({ viewAreaCoveragePercentThreshold: 50 })
    
    
    <FlatList
          horizontal={true}
          onViewableItemsChanged={onViewRef.current}
          data={Object.keys(cards)}
          keyExtractor={(_, index) => index.toString()}
          viewabilityConfig={viewConfigRef.current}
          renderItem={({ item, index }) => { ... }}
    />
    

    关于react-native - 任何状态更改时的 FlatList ScrollView 错误 - 不变违规 : Changing onViewableItemsChanged on the fly is not supported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48045696/

    相关文章:

    javascript - 在两个可拖动的 Flatlist-React Native 之间拖放

    javascript - 在平面列表中 react native 导航

    onclick - 将子事件暴露给父事件

    android - 第三方 API 请求在生产中失败,但在暂存中有效。如何解决这个问题?

    react-native - React-Native FlatList 中的 getItemLayout

    react-native - FlatList numColumns 似乎无法正常工作?

    react-native - 嵌套平面列表不变违规 : A VirtualizedList contains a cell which itself contains more than one VirtualizedList

    javascript - React Native 不支持在 Windows 上开发(还)?

    ios - 仅在 AppDelegate.m iOS 中首次启动时设置特定方向

    javascript - 构建 JavaScript 包失败。无法读取 react native 模块 traverseDependencies.js 上未定义的属性 'reduce'