TouchableOpacity onPress problems inside a ScrollView

See original GitHub issue

This issue was originally posted on StackOverflow but no-one has given a solution for it. On the other hand, users have verified that the issue is there and is present on both iOS and Android: See: http://stackoverflow.com/questions/36822391/react-native-touchableopacity-onpress-problems-inside-a-scrollview

I am running react native 0.24.1 and I am experiencing an issue with the <TouchableOpacity> component when it is placed inside an <ScrollView>.

Its onPress events fire fine but there is a special case when they do not. If along with the <TouchableOpacity> component you have a <TextInput>, and the current focus is on the <TextInput> box, then you may click on the <TouchableOpacity> and you will see its onPress event WILL NOT be fired.

At least the first time you do it. Once the focus is NOT on the <TextInput> anymore, you can now press on the <TouchableOpacity> component and its onPress event will fire just fine.

Note that if the <TouchableOpacity> component is placed inside a <View> instead of an <ScrollView> everything works as expected and the above issue does not apply.

Here is some code to demonstrate the problem:

const React = require('react-native');
const {
  Component,
  Dimensions,
  View,
  ScrollView,
  Text,
  TextInput,
  TouchableOpacity,
} = React;


// ----------------------------------------------------------------------------
class TouchableOpacityTest extends Component {
  constructor(props, context) {
    super(props, context);
    this.state = {count_onPress:0,count_onPressIn:0,count_onPressOut:0,count_onLongPress:0};
  }
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  onPressEvent(what,e) {
    console.log('what:',what);
    let newState = {};
    newState['count_'+what] = ++this.state['count_'+what];
    this.setState(newState);
  }
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  render() {
    let touchableProps = {
      onPress: this.onPressEvent.bind(this,'onPress'),
      onPressIn: this.onPressEvent.bind(this,'onPressIn'),
      onPressOut: this.onPressEvent.bind(this,'onPressOut'),
      onLongPress: this.onPressEvent.bind(this,'onLongPress'),
    }

    return (
      <View style={{flex:1,flexDirection:'column',justifyContent:'flex-start',alignItems:'center',backgroundColor:'blue'}} >
        <ScrollView style={{width:Dimensions.get('window').width*0.9,backgroundColor:'red'}}>
          <TextInput style={{backgroundColor:'rgb(200,200,200)',marginTop:14}}
            placeholder="Focus on me,hide keyboard,and click on text below"
            autoCorrect={false}
          />
          <TouchableOpacity {...touchableProps} >
            <Text style={{fontSize:20,backgroundColor:'pink',marginTop:14}}>
              Click on me!{"\n"}
              onPress:{this.state.count_onPress}{"\n"}
              onPressIn:{this.state.count_onPressIn}{"\n"}
              onPressOut:{this.state.count_onPressOut}{"\n"}
              onLongPress:{this.state.count_onLongPress}{"\n"}
            </Text>
          </TouchableOpacity>
        </ScrollView>
      </View>
    );
  }
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
// ----------------------------------------------------------------------------
AppRegistry.registerComponent('react_native_app1', () => TouchableOpacityTest);

You may replace the <ScrollView> with a <View> component on the above code and you will see that onPress event fires every time, even when the focus is on the <TextView>

According to some users, this is ALSO happening on iOS too.

Thanks. Alex

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:12
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

14reactions
npomfretcommented, Jun 16, 2016

Here’s a sample project that shows the solution, which is simply to use

<ScrollView keyboardShouldPersistTaps={true}>

See example 3.

5reactions
ThaJaycommented, Jun 22, 2016
<ScrollView
  scrollEnabled={true}
  showsVerticalScrollIndicator={true}
  keyboardShouldPersistTaps={true}
  keyboardDismissMode='on-drag'
>
Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native: TouchableOpacity onPress problems inside a ...
'always', the keyboard will not dismiss automatically, and the scroll view will not catch taps, but children of the scroll view can catch...
Read more >
TouchableOpacity inside ScrollView trigger onPress while ...
I have a View with FlatList each item in list is TouchableOpacity. Until now I was able to scroll list and when stop...
Read more >
React Native ScrollView Inside Touchable and How to Make it ...
Having a ScrollView inside any kind of Touchable cause a very common problem in the Android platform. The problem is that you can...
Read more >
prevent touch parent on children view react native - You.com
In my case I simply put the View inside another TouchableOpacity (with ... The issue is that Touchable*#onPress doesn't work on iOS devices...
Read more >
Handling Touches - React Native
Pressing the button will call the "onPress" function, which in this ... TouchableOpacity can be used to provide feedback by reducing the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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