简体   繁体   English

获取 ReferenceError:由于 onPress 中的方法,找不到变量:TouchableOpacity

[英]Getting ReferenceError: Can't find variable: TouchableOpacity due to a method in onPress

This is with reference to a react native app.这是参考本机应用程序。 I have created a navigation reference file to navigate to screens from components not part of navigator.我创建了一个导航参考文件,用于从不属于导航器的组件导航到屏幕。 Below is the code for it.下面是它的代码。

import { NavigationActions } from 'react-navigation';

export const setNavigator = (nav) => {
    navigator = nav;
}

export const navigate = (routeName, params) => {
    navigator.dispatch(
        NavigationActions.navigate({
            routeName: routeName,
            params: params
        })
    );
};

to use it I just import import {navigate} from '../../src/navigationRef';要使用它,我只需import {navigate} from '../../src/navigationRef';导入import {navigate} from '../../src/navigationRef'; and navigate('ScreenName');navigate('ScreenName');

I have a made one component, whenever this component is clicked it should navigation to QuestionScreen, below is the code我有一个单独的组件,每当点击这个组件时,它应该导航到 QuestionScreen,下面是代码

return (
        <TouchableOpacity  onPress={() => {navigate('QuestionScreen')}}>
        <View style={{marginTop: 8, backgroundColor: 'white', padding: 8}}>
            <Text style={styles.questionStyle} >{question}</Text>
            <View style={{ flexDirection: 'row', justifyContent: 'space-between', marginTop: 16 }} >
                <Text style={styles.nameStyle} >{name}</Text>
                <Text style={styles.timeStyle} > 2 days ago </Text>
            </View>
            <View style={{ flexDirection: 'row', marginTop: 16, justifyContent: 'space-between' }} >
                <View style={{ flexDirection: 'row' }} >
                    <Text style={{ fontSize: 12, color: '#6C6C6C' }} >Answers {noOfAnswers}</Text>

                    <View style={{flexDirection: 'row', marginLeft: 16}}>
                        {/* <MaterialIcons name="trending-up" size={24} color="#CA534C" /> */}
                        <Image   style={{width: 24, height:  24}} source={ require('../../assets/heart-pulse-line.png')} />
                        <Text style={{ fontSize: 12, color: '#6C6C6C' }} >{noOfInsightfuls}</Text>
                    </View>
                </View>
                <Text style={{ fontSize: 12, fontWeight: 'bold', color: '#CA534C' }} >{tag}</Text>
            </View>
        </View>
        </TouchableOpacity>
    );

tapping this component results in below error点击此组件会导致以下错误

ReferenceError: Can't find variable: TouchableOpacity参考错误:找不到变量:TouchableOpacity

node_modules\\react-native\\Libraries\\LogBox\\LogBox.js:148:8 in registerError node_modules\\react-native\\Libraries\\LogBox\\LogBox.js:59:8 in errorImpl node_modules\\react-native\\Libraries\\LogBox\\LogBox.js:33:4 in console.error node_modules\\expo\\build\\environment\\react-native-logs.fx.js:27:4 in error node_modules\\react-native\\Libraries\\Core\\ExceptionsManager.js:104:6 in reportException node_modules\\react-native\\Libraries\\Core\\ExceptionsManager.js:171:19 in handleException node_modules\\react-native\\Libraries\\Core\\setUpErrorHandling.js:24:6 in handleError node_modules\\expo-error-recovery\\build\\ErrorRecovery.fx.js:9:32 in ErrorUtils.setGlobalHandler$argument_0 node_modules\\regenerator-runtime\\runtime.js:63:36 in tryCatch node_modules\\regenerator-runtime\\runtime.js:293:29 in invoke node_modules\\regenerator-runtime\\runtime.js:63:36 in tryCatch node_modules\\regenerator-runtime\\runtime.js:154:27 in invoke node_modules\\regenerator-runtime\\runtime.js:164:18 in PromiseImpl.resolve.then$argument_0 node node_modules\\react-native\\Libraries\\LogBox\\LogBox.js:148:8 in registerError node_modules\\react-native\\Libraries\\LogBox\\LogBox.js:59:8 in errorImpl node_modules\\react-native\\Libraries\\LogBox\\LogBox。 js:33:4 in console.error node_modules\\expo\\build\\environment\\react-native-logs.fx.js:27:4 in error node_modules\\react-native\\Libraries\\Core\\ExceptionsManager.js:104:6 in reportException node_modules\\react-native\\Libraries\\Core\\ExceptionsManager.js:171:19 in handleException node_modules\\react-native\\Libraries\\Core\\setUpErrorHandling.js:24:6 in handleError node_modules\\expo-error-recovery\\build\\ErrorRecovery .fx.js:9:32 in ErrorUtils.setGlobalHandler$argument_0 node_modules\\regenerator-runtime\\runtime.js:63:36 in tryCatch node_modules\\regenerator-runtime\\runtime.js:293:29 invoke node_modules\\regenerator-runtime\\ runtime.js:63:36 in tryCatch node_modules\\regenerator-runtime\\runtime.js:154:27 invoke node_modules\\regenerator-runtime\\runtime.js:164:18 in PromiseImpl.resolve.then$argument_0 节点_modules\\react-native\\node_modules\\promise\\setimmediate\\core.js:37:13 in tryCallOne node_modules\\react-native\\node_modules\\promise\\setimmediate\\core.js:123:24 in setImmediate$argument_0 node_modules\\react-native\\Libraries\\Core\\Timers\\JSTimers.js:130:14 in _callTimer node_modules\\react-native\\Libraries\\Core\\Timers\\JSTimers.js:181:14 in _callImmediatesPass node_modules\\react-native\\Libraries\\Core\\Timers\\JSTimers.js:441:30 in callImmediates node_modules\\react-native\\Libraries\\BatchedBridge\\MessageQueue.js:387:6 in __callImmediates node_modules\\react-native\\Libraries\\BatchedBridge\\MessageQueue.js:135:6 in __guard$argument_0 node_modules\\react-native\\Libraries\\BatchedBridge\\MessageQueue.js:364:10 in __guard node_modules\\react-native\\Libraries\\BatchedBridge\\MessageQueue.js:134:4 in flushedQueue _modules\\react-native\\node_modules\\promise\\setimmediate\\core.js:37:13 in tryCallOne node_modules\\react-native\\node_modules\\promise\\setimmediate\\core.js:123:24 in setImmediate$argument_0 node_modules\\react-native\\ Libraries\\Core\\Timers\\JSTimers.js:130:14 in _callTimer node_modules\\react-native\\Libraries\\Core\\Timers\\JSTimers.js:181:14 in _callImmediatesPass node_modules\\react-native\\Libraries\\Core\\Timers\\JSTimers。 js:441:30 in callImmedates node_modules\\react-native\\Libraries\\BatchedBridge\\MessageQueue.js:387:6 in __callImmediates node_modules\\react-native\\Libraries\\BatchedBridge\\MessageQueue.js:135:6 in __guard$modules\\react node_0 -native\\Libraries\\BatchedBridge\\MessageQueue.js:364:10 in __guard node_modules\\react-native\\Libraries\\BatchedBridge\\MessageQueue.js:134:4 influshedQueue

  • [native code]:null in flushedQueue [本机代码]:flushedQueue 中的 null
  • [native code]:null in invokeCallbackAndReturnFlushedQueue [本机代码]:invokeCallbackAndReturnFlushedQueue 中的 null

if I use a console.log in onPress method of TouchableOpacity, it works fine.*如果我在 TouchableOpacity 的 onPress 方法中使用console.log ,它工作正常。*

您必须从 react native 导入 TouchableOpacity ,如下所示

import {TouchableOpacity} from 'react-native;

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

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