React Native onStartShouldSetResponder and onResponderRelease?

混江龙づ霸主 提交于 2020-01-24 09:44:45

问题


I've created a button that I want to have call a function on click and then again on release. A normal TouchableOpacity or other will trigger a function upon release of a click only, I need functions on both click AND release.

<View
    style={styles.touchbutton}
    onStartShouldSetResponder={() => this.clickOn()}
    onResponderRelease={() => this.clickRelease()}>
    <Text style={styles.dark}>Button</Text>
</View>

The above code works on click but not on release. I also tried onResponderReject but that doesn't work either. Hard to find what the command should be.


回答1:


Had the same problem. onStartShouldSetResponder needs to return true.

onStartShouldSetResponder={(e) => {
    /*do whatever*/;
    return true
}}



回答2:


Please Try This code For your click event it works for me.

clickOn(){
Alert.alert(
  'Test Demo',
  'Please Enter Valid Data',
  [
    //{text: 'Ask me later', onPress: () => console.log('Ask me later pressed')},
    {text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
    {text: 'OK', onPress: () => console.log('OK Pressed')},
  ],
  { cancelable: false }
)
  }
}

PUT THIS TWO LINE IT WOKS FOR ME WHEN YOU RELEASE

  onStartShouldSetResponder={() => true}
  onResponderRelease={() => this.onRowTap()}


来源:https://stackoverflow.com/questions/36758659/react-native-onstartshouldsetresponder-and-onresponderrelease

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!