How to call PanResponder handlers with FlatList index - Stack Overflow

I use PanResponder on a FlatList of components.Each one is injected with the panHandlers from a single

I use PanResponder on a FlatList of components. Each one is injected with the panHandlers from a single created PanResponder (PanResponder.create). Because the list can potentially grow into the hundreds, I didn't want hundreds of responders. All the components share the same panHandlers.

const panResponder = PanResponder.create();
const panHandlers = useRef(panResponder?.panHandlers);

<FlatList
  renderItem={
    <View>
      <DragHandle
        index={index}
        panHandlers={panHandlers.current}
      />
    </View>
  }
/>

Note that the item index is available at the DragHandle level. However I don't have access to index in the handler itself and so then need to resort to complex calculations which seem really redundant. Furthermore, the accuracy of those calculations depends on assumptions, like constant list item height for example. (The yToIndex function included in the handler below uses rowHeightRef.current).

onPanResponderGrant: (evt, gestureState) => {
    ...
    currIdxRef.current = yToIndex(gestureState.y0);
  },

const yToIndex = 
    ...
  const index = Math.min(
    (filteredSongIds?.length ?? 0) - 1,
    Math.max(
      0,
      Math.floor(
        (scrollOffsetYRef.current + y - flatListParentYRef.current) /
          rowHeightRef.current
    ...

);

Normally this is not a problem but recently I wanted to add line breaks to the UI of the list. This caused the index of every item after the first break to be incorrectly calculated due the extra height space of the breaks.

How can I inject index into the handler?

Or do I really need to create multiple Responders? If so, the previous question still stands.

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

相关推荐

  • How to call PanResponder handlers with FlatList index - Stack Overflow

    I use PanResponder on a FlatList of components.Each one is injected with the panHandlers from a single

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信