question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Closing a Swipeable element when another is opened

See original GitHub issue

I’m currently using Swipeable to create a list of items, each of which the user can swipe to delete. It’s working fine but if I swipe a row and open one and then swipe another, the previously-opened row is still swiped open but I’d like it to be closed.

I’ve searched for solutions and made a little progress myself using refs but I’ve not been able to get it working perfectly. I’ve also come across quite a few people asking for the same thing and it seems like pretty standard behaviour. It’d be great if you could add some documentation with examples covering this.

Thank you.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:23
  • Comments:41 (2 by maintainers)

github_iconTop GitHub Comments

70reactions
annieweycommented, Jan 3, 2020

I tried using ref as well and it seems to be working fine except that the close animation is delayed a little.

can you show us your workaround ?

@AmanSharma2609 The idea is using ref and close(). Hope it helps

let row: Array<any> = [];
let prevOpenedRow;

renderItem ({ item, index }) {
  return (
    <Swipeable
	ref={ref => row[index] = ref}
	friction={2}
	leftThreshold={80}
	rightThreshold={40}
	renderRightActions={renderRightActions}
	containerStyle={style.swipeRowStyle}
        onSwipeableOpen={closeRow(index)}
	...
    >
    ...
    </Swipeable>);
}

closeRow(index) {
    if (prevOpenedRow && prevOpenedRow !== row[index]) {
		prevOpenedRow.close();
    }
    prevOpenedRow = row[index];
}
37reactions
Lsleimancommented, May 28, 2020

This seems to be working for me. Sharing just in case

 let rowRefs = new Map();
  
  const renderItem = ({item}) => (
  <Swipeable
    key={item.key}
    ref={ref => {
      if (ref && !rowRefs.get(item.key)) {
        rowRefs.set(item.key, ref);
      }
    }}
    onSwipeableWillOpen={()=>{
        [...rowRefs.entries()].forEach(([key, ref]) => {
          if (key !== item.key && ref) ref.close();
        });
     }}
     >
   </Swipeable>
  );
Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native Swipeable (Swipe to delete) not closing
When I press delete, the contact gets deleted however the swipeable remains open. I want it to close after it gets pressed but...
Read more >
Swipeable | React Native Gesture Handler - Software Mansion
onSwipeableOpen ​. method that is called when action panel gets open (either right or left). Takes swipe direction as an argument. onSwipeableClose ​....
Read more >
React Native Mobile App Tutorial: Using Swipeable ... - YouTube
React Native Mobile App Tutorial: Using Swipeable Component With FlatList #reactnative #supabase #flatlist #swipeableSo the last video was ...
Read more >
React Native Gesture Handler - Swipe To Delete - YouTube
React Native Swipeable | Swipe To Delete | React Native Gesture Handler | Dope Programming · Key moments. View all · Key moments...
Read more >
A ListView With rows that Swipe Open and Closed - Morioh
Handles default native behavior such as closing rows when ListView is ... Also includes <SwipeRow> if you want to use a swipeable row...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found