首页 > 解决方案 > 以编程方式触发 TouchableOpacity.onPress

问题描述

我有一个用例,文件上传后我想打开包裹在里面的灯箱<TouchableOpacity>

这是我的代码。

<TouchableOpacity style={{marginRight: ms(10), borderRadius: ms(10), overflow: 'hidden'}}>
    <Lightbox>
        <FastImage
            style={{width: ms(75), height: ms(75), borderRadius: ms(10)}}
            source={{uri: this.props.image.path}}
        />
    </Lightbox>
</TouchableOpacity>

灯箱没有任何以编程方式触发打开的事件,这是我正在使用的灯箱https://github.com/oblador/react-native-lightbox

我唯一的选择似乎是TouchableOpacity.onPress在文件上传完成后以编程方式触发,尝试使用 ref 以下方式。

TouchableOpacity ref={component => this.touchable = component}这给了this.touchable.props.onPress();我一个错误,说未定义不是一个对象,当console.log(this.touchable)我注意到它不是空的,但是当我这样做时console.log(this.touchable.props)它说未定义,也许语法发生了变化,这似乎没有记录在案。

有没有办法以编程方式触发 onPress?

谢谢。

标签: react-nativetouchableopacity

解决方案


Maybe try :

ref={(touchable) => this._touchable = touchable}
this._touchable.touchableHandlePress()  //this?

推荐阅读