简体   繁体   English

可触摸不透明度在堆栈导航器屏幕中没有响应 - React Native

[英]Touchable Opacity not responding in stack navigator screen - React Native

I'm building a React Native app, it uses React Navigation.我正在构建一个 React Native 应用程序,它使用 React Navigation。 I use TouchableOpacity throughout the app, however, in a stack navigator screen, it doesn't seem to work at all.我在整个应用程序中都使用 TouchableOpacity,但是,在堆栈导航器屏幕中,它似乎根本不起作用。 Touching the element doesn't change the opacity and the onpress function doesn't work.触摸元素不会改变不透明度,并且 onpress 功能不起作用。 The screen itself displays fine and all other screens in my app have TouchableOpacity's that work fine.屏幕本身显示良好,我的应用程序中的所有其他屏幕都具有可以正常工作的 TouchableOpacity。

Using button doesn't respond either, I'm thinking this is a react navigation issue potentially?使用按钮也没有响应,我认为这可能是一个反应导航问题? There is no issues transitioning to the screen though?过渡到屏幕没有问题吗?

Here is my screen;这是我的屏幕;

import React, {Component} from 'react';
import { View, Text, TouchableOpacity, Alert, Button}  from 'react-native';

class RaceScreen extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
        }
    }
    render() {
        return (
            <View style={{ flex: 1, alignItems: 'center', backgroundColor:'rgba(30,30,30,0.98)'}}>
                <TouchableOpacity onPress = {() => console.log('Hello')}>
                    <View style={{ margin:50, height:100, width: 200, backgroundColor:'red', alignItems:'center', justifyContent:'center' }}>
                        <Text style={{ color:'white' }}>
                            Go back
                        </Text>
                    </View>
                </TouchableOpacity>
                <Button title="Go back button" onPress = {() => console.log('Hello')}>
                </Button>
            </View>
        );
    }
}

export default RaceScreen

I've found that the Touchable components typically don't respond well to text children.我发现Touchable组件通常不能很好地响应文本子项。 You simply need to wrap it inside a View :您只需要将其包装在View

import React, {Component} from 'react';
import { View, Text, TouchableOpacity, Alert}  from 'react-native';

export default class RaceScreen extends React.Component {

    render() {
        return (
            <View style={{ flex: 1, alignItems: 'center', backgroundColor:'rgba(30,30,30,0.98)'}}>
                <TouchableOpacity onPress = {() => console.log('Hello')}>
                    <View style={{ margin:50, height:100, width: 200, backgroundColor:'red', alignItems:'center', justifyContent:'center' }}>
                        <Text style={{ color:'white' }}>
                            Go back
                        </Text>
                    </View>
                </TouchableOpacity>
            </View>
        );
    }
}

I finally figured it out.我终于弄明白了。 In the createStackNavigator method from react-navigation, transparentCard:true is a deprecated property and was causing the bug.在 react-navigation 的 createStackNavigator 方法中,transparentCard:true 是一个已弃用的属性,并导致了错误。 I was using version 3 documentation on a version 4 package of react navigation.我在版本 4 的 React 导航包上使用了版本 3 文档。

Looking at there site, they have just released version 5 which is great!看看那里的网站,他们刚刚发布了第 5 版,这很棒!

A note to the less experienced developers like myself, making sure you're aware of the version of each package you are using is critical for some of these difficult bugs.对像我这样经验不足的开发人员的说明,确保您了解所使用的每个软件包的版本对于其中一些困难的错误至关重要。 Don't let it get you down though, react native is elite!不要让它让你失望,反应本土是精英!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM