【问题标题】:Determine what screen is on?确定在什么屏幕上?
【发布时间】:2018-09-14 12:52:54
【问题描述】:

好的,所以我有两个屏幕,当 qrscanner 读取 qr 时它会导航到 InfoScreen,这一切都很好,但问题是即使屏幕是 InfoScreen,如果相机指向 qr 码它仍然会扫描。

我的问题是如何检查现在的屏幕应用程序,以便我可以在 qrCodeOnReadHandler if(!qrscreen){return} 中编写类似的内容

export default class Qr extends Component {
    qrCodeOnReadHandler = ({ data }) => {
        this.props.navigation.navigate("InfoScreen")
    };
}

class InfoScreen extends Component {
    render() {
        return (
            <View style={styles.buttons}>
              <TouchableOpacity
                style={styles.buttonStyle}
                onPress={() => this.props.navigation.goBack()}
              >
                <Text style={styles.textStyle}>Back</Text>
              </TouchableOpacity>
            </View>
        );
    }
}

【问题讨论】:

    标签: react-native react-native-navigation react-native-navigation-v2


    【解决方案1】:

    您可以注册一个在组件聚焦或模糊时触发的事件监听器:

     componentWillMount() {
           this.props.navigation.addListener('willBlur', () =>this.doSomethingOnBlur());
           this.props.navigation.addListener('onFocus', ()=>this.doSomethingOnFocus());
    }
    

    你可以在这里找到所有的事件

    https://reactnavigation.org/docs/en/navigation-prop.html

    【讨论】:

    • 谢谢。我实际上在 qrCodeOnReadHandler 方法中添加了返回布尔值的 !this.props.navigation.isFocused 函数,将它放在 if 语句中然后返回,它工作得很好......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-30
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    相关资源
    最近更新 更多