以编程方式触发TouchableOpacity.onPress

时间:2020-06-04 06:00:21

标签: react-native touchableopacity

我有一个用例,在文件上传后,我想打开包裹在<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();一起给我一个错误,指出undefined不是对象,当我执行console.log(this.touchable)时,我发现它并不为空,但是当我执行console.log(this.touchable.props)它表示未定义,也许语法已更改,但似乎没有记录。

有什么方法可以以编程方式触发onPress吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

也许尝试:

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