35

I am developing a simple React Native application for learning purpose. I am just taking my initial step to get into the React Native world. But in this very early stage, I am having problems. I cannot get a simple touch event working. I am implementing touch event using TouchableWithoutFeedback. This is my code.

class AlbumList extends React.Component {

    constructor(props)
    {
        super(props)
        this.state = {
            displayList : true
        }
    }
    componentWillMount() {
        this.props.fetchAlbums();
    }

    albumPressed(album)
    {
        console.log("Touch event triggered")
    }

    renderAlbumItem = ({item: album}) => {
        return (
                <TouchableWithoutFeedback onPress={this.albumPressed.bind(this)}>
                    <Card>
                        <CardSection>
                            <Text>{album.artist}</Text>
                        </CardSection>
                        <CardSection>
                            <Text>{album.title}</Text>
                        </CardSection>
                    </Card>
                </TouchableWithoutFeedback>
            )
    }

    render() {
        let list;
        if (this.state.displayList) {
            list = <FlatList
                data={this.props.albums}
                renderItem={this.renderAlbumItem}
                keyExtractor={(album) => album.title}
            />
        }

        return (
            list
        )
    }
}

const mapStateToProps = state => {
    return state.albumList;
}

const mapDispatchToProps = (dispatch, ownProps) => {
    return bindActionCreators({
        fetchAlbums : AlbumListActions.fetchAlbums
    }, dispatch)
}

export default connect(mapStateToProps, mapDispatchToProps)(AlbumList);

As you can see, I am implementing touch event on the list item. But it is not triggering at all when I click on the card on Simulator. Why? How can I fix it?

Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372

7 Answers7

59

You should wrap your content in component like this:

<TouchableWithoutFeedback>
 <View>
  <Your components...>
 </View>
</TouchableWithoutFeedback>
Turkysha
  • 614
  • 7
  • 3
  • 5
    The [github issue](https://github.com/facebook/react-native/issues/10180#issuecomment-298375648) has already been discussed in the comments. Perhaps you can fill in the other details so it's all in one place. Simply posting a code snippet without an explanation is rarely helpful. – Boyan Hristov Feb 20 '20 at 23:34
  • I have to search first for any issue or errors instead of figuring out by myself and wasting huge time on small things. which already has been known issues.... by the why thanks for posting this answer. – dipenparmar12 Dec 06 '20 at 08:43
30

TouchableWithoutFeedback always needs to have child View component. So a component that composes a View isn't enough.

So instead of

<TouchableWithoutFeedback onPressIn={...} onPressOut={...} onPress={...}>
  <MyCustomComponent />
</TouchableWithoutFeedback>

use:

<TouchableWithoutFeedback onPressIn={...} onPressOut={...} onPress={...}>
  <View>
    <MyCustomComponent />
  </View>
</TouchableWithoutFeedback>

See the github issue for more info

Nabeel K
  • 5,938
  • 11
  • 38
  • 68
13

Can be used with <TouchableOpacity activeOpacity={1.0}> </TouchableOpacity>

Avijit
  • 1,253
  • 13
  • 21
2

For those who struggle with this issue in react-native 0.64, and wrapping it in just a View doesn't work, try this:

  <TouchableWithoutFeedback onPress={onPress}>
    <View pointerEvents="none">
      <Text>Text</Text>
    </View>
  </TouchableWithoutFeedback>
duskload
  • 21
  • 3
2

In more recent React Native versions, just use Pressable instead: https://reactnative.dev/docs/pressable

Andreas Bergström
  • 13,891
  • 5
  • 59
  • 53
1

In my case i accidentally imported TouchableWithoutFeedback from react-native-web instead of react-native. After importing from react-native everything worked as expected.

emrich
  • 145
  • 1
  • 9
-1

In my case, there was a shadow underneath, which caused instability. What I did to solve it was quite simple: zIndex: 65000

<View style={{ zIndex: 65000 }}>
   <TouchableWithoutFeedback onPressIn={() =>  {}>
     <View>
     </View>
   </TouchableWithoutFeedback>
</View>