React native onPress with TouchableWithoutFeedback不起作用

问题描述 投票:2回答:1

我正在开发一个用于学习目的的简单React Native应用程序。我只是迈出第一步进入React Native世界。但是在这个非常早期的阶段,我遇到了问题。我无法获得一个简单的触摸事件。我正在使用TouchableWithoutFeedback实现触摸事件。这是我的代码。

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);

如您所见,我正在列表项上实现触摸事件。但是,当我单击Simulator上的卡时,它根本不会触发。为什么?我该如何解决?

react-native touchablewithoutfeedback
1个回答
0
投票

您应该将内容包装在这样的组件中:

<TouchableWithoutFeedback>
 <View>
  <Your components...>
 </View>
</TouchableWithoutFeedback>
© www.soinside.com 2019 - 2024. All rights reserved.