React Native Prevent Double Tap

后端 未结 10 539
醉梦人生
醉梦人生 2020-12-30 02:22

I have a TouchableHighlight wrapping a Text block that when tapped, opens a new scene (which I\'m using react-native-router-flux).
It\'s all wo

相关标签:
10条回答
  • 2020-12-30 02:56

    I fixed using this lodash method below,

    Step 1

    import { debounce } from 'lodash';
    

    Step 2

    Put this code inside the constructor

    this.loginClick = debounce(this.loginClick .bind(this), 1000, {
                leading: true,
                trailing: false,
    });
    

    Step 3

    Write on your onPress button like this

    onPress={() => this.props.skipDebounce ? this.props.loginClick : this.loginClick ()}
    

    Thanks,

    0 讨论(0)
  • 2020-12-30 02:59

    The following works by preventing routing to the same route twice:

    import { StackNavigator, NavigationActions } from 'react-navigation';
    
    const App = StackNavigator({
        Home: { screen: HomeScreen },
        Details: { screen: DetailsScreen },
    });
    
    // Prevents double taps navigating twice
    const navigateOnce = (getStateForAction) => (action, state) => {
        const { type, routeName } = action;
        return (
            state &&
            type === NavigationActions.NAVIGATE &&
            routeName === state.routes[state.routes.length - 1].routeName
        ) ? state : getStateForAction(action, state);
    };
    App.router.getStateForAction = navigateOnce(App.router.getStateForAction);
    
    0 讨论(0)
  • 2020-12-30 02:59

    You can do using debounce very simple way

    import debounce from 'lodash/debounce';
    
    componentDidMount() {
    
           this.yourMethod= debounce(this.yourMethod.bind(this), 500);
      }
    
     yourMethod=()=> {
        //what you actually want your TouchableHighlight to do
    }
    
    <TouchableHighlight  onPress={this.yourMethod}>
     ...
    </TouchableHighlight >
    
    0 讨论(0)
  • 2020-12-30 03:00

    If you are using react-navigation, you can supply a key property to navigate to ensure only one instance is ever added to the stack.

    via https://github.com/react-navigation/react-navigation/issues/271

    0 讨论(0)
提交回复
热议问题