kkemple/react-native-sideswipe

Cancel swipe

ruben2807 opened this issue · 5 comments

How can I do to prevent the user from moving around the different views? and manage the current changes Index for my own.
Basically what I want is to cancel the swipe.

Can someone help me?

Use the onIndexChange callback to check if the index is in your allowed range. If not, set the index prop back to the previous allowed index.

Hi rubycon!
Yes, that could help me. The problem is that this event is executed when the transition and the slide change are carried out. I would like to remove the user the power to move the slide.

Yes, the event is fired after the change but if you update the index in response, SideSwipe won't start rendering the transition. It might look a bit hacky but works great and render nice on Android and iOS for me:

<SideSwipe
  // ...
  index={this.state.index}
  onIndexChange={index => {
    const prevIndex = this.state.index;
    // update your index state in any case, to keep the value coherent
    // with the internal state of SideSwipe. This won't trigger another onIndexChange.
    this.setState({index}, () => {
      // then correct the index if needed.
      if(isNotAllowed(index))
        this.setState({index: prevIndex});
    })
  }}
/>

If you want to deny the move you can use the shouldCapture callback and test if the user is swiping left or right of the current index. Maybe something like (not tested):

shouldCapture={gestureState => {
  if(gestureState.dx > 0 && isNotAllowed(this.index + 1))
    return false;

  if(gestureState.dx < 0 && isNotAllowed(this.index - 1))
    return false;

  return true;
}}

Thank you very much, with that I can work perfectly.
Excuse my bad English, greetings from Costa Rica.

I'm having a similar issue; where I want to make sure that the user cannot interact with the side-swipe component, but returning false within shouldCapture is not disabling this currently. I recently updated to RN V-59.1 and ReactNativeNavigation V-2.22.3, and this behavior started. Are there any other props on Sideswipe you would recommend to block a users gestures?