代码之家  ›  专栏  ›  技术社区  ›  techie_questie

React本机二维码扫描器抛出错误

  •  2
  • techie_questie  · 技术社区  · 7 年前

    我有一个react native应用程序,我在其中开发了一个使用react native qrcode的扫描仪功能-但是,扫描仪,当我尝试扫描数据时,得到以下错误-

    error: can't find variable navigation
    

    我在onSuccess方法的authorizationToken行看到这个错误。 我的代码-

    import React, { Component } from 'react';
    import {
        Text,
        View,
        Image,
        TouchableOpacity,
        Linking
    } from 'react-native';
    import styles from '../assets/style';
    import QRCodeScanner from 'react-native-qrcode-scanner';
    
    export default class ScanScreen extends Component {
    
        onSuccess(scanEvent) {
            this.props.navigation.navigate("Result", {
                'accessKey': scanEvent.data,
                'authorizationToken':navigation.getParam('authorizationToken', undefined), 
                "userData": navigation.getParam('userData', undefined),
                "methodName": "fetchData"
            });
        }
    
        render() {
            return (
                <View style={styles.container}>
                    <QRCodeScanner
                        onRead={this.onSuccess.bind(this)}
                    />
                </View>
            );
        }
    }
    

    你知道我在这里遗漏了什么吗。任何帮助都很重要谢谢,谢谢提前。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Paweł Mikołajczuk    7 年前

    确保您的屏幕已在react navigation config中注册(遵循以下指南:找不到变量导航)。

    或者用HOC把导航道具传给它 withNavigation : https://reactnavigation.org/docs/en/with-navigation.html . 相反 export default class ScanScreen extends Component class ScanScreen extends Component 在文件的末尾

    export default withNavigation(ScanScreen);
    

    不要忘记导入高阶组件: import { withNavigation } from 'react-navigation';

    还要确保所有本机部分都正确链接。例如 react-native-gesture-handle ( https://kmagiera.github.io/react-native-gesture-handler/docs/getting-started.html#linking ).

        2
  •  0
  •   techie_questie    7 年前

    导航必须是道具的一部分,因此使用this.props.导航解决了这个问题。