简体   繁体   English

React Native - TypeError:undefined 不是评估 _useContext 的对象

[英]React native - TypeError: undefined is not an object evaluating _useContext

So this is a bit of a weird case.所以这是一个有点奇怪的案例。 My app works totally fine and context works fine as well.我的应用程序运行良好,上下文也运行良好。 However;然而; whenever I save my apiHelpers.js file (an object with various api calls) in debug mode, my app crashes.每当我在调试模式下保存 apiHelpers.js 文件(具有各种 api 调用的对象)时,我的应用程序就会崩溃。

Every save - regardless of if I changed something or not crashes the app.每次保存 - 无论我是否更改了某些内容,都会使应用程序崩溃。 Just cmd + S on the file is making it crash.文件上的cmd + S使它崩溃。 If it's just a weird debug mode - fine, I can put up with it.如果这只是一个奇怪的调试模式 - 好吧,我可以忍受。 My concern is if it could be a symptom of a problem that could cause the app to crash in production.我担心的是,这是否可能是导致应用程序在生产中崩溃的问题的征兆。

The error:错误:

TypeError: undefined is not an object (evaluating '_useContext2.getData')

This error is located at:
    in MyDashboard (at SceneView.js:9)
    in SceneView (at createTabNavigator.tsx:67)
    in RCTView (at ResourceSavingScene.tsx:37)
    in RCTView (at ResourceSavingScene.tsx:26)
    in ResourceSavingScene (at createBottomTabNavigator.tsx:157)
    in RCTView (at screens.native.js:101)
    in ScreenContainer (at createBottomTabNavigator.tsx:147)
    in RCTView (at createBottomTabNavigator.tsx:146)
    in TabNavigationView (at createTabNavigator.tsx:228)
    in NavigationView (at createNavigator.js:80)
    in Navigator (at SceneView.js:9)
    in SceneView (at StackViewLayout.tsx:900)
    in RCTView (at createAnimatedComponent.js:151)
    in AnimatedComponent (at StackViewCard.tsx:106)
    in RCTView (at createAnimatedComponent.js:151)
    in AnimatedComponent (at screens.native.js:71)
    in Screen (at StackViewCard.tsx:93)
    in Card (at createPointerEventsContainer.tsx:95)
    in Container (at StackViewLayout.tsx:975)
    in RCTView (at screens.native.js:101)
    in ScreenContainer (at StackViewLayout.tsx:384)
    in RCTView (at createAnimatedComponent.js:151)
    in AnimatedComponent (at StackViewLayout.tsx:374)
    in PanGestureHandler (at StackViewLayout.tsx:367)
    in StackViewLayout (at withOrientation.js:30)
    in withOrientation (at StackView.tsx:104)
    in RCTView (at Transitioner.tsx:267)
    in Transitioner (at StackView.tsx:41)
    in StackView (at createNavigator.js:80)
    in Navigator (at createKeyboardAwareNavigator.js:12)
    in KeyboardAwareNavigator (at SceneView.js:9)
    in SceneView (at SwitchView.js:12)
    in SwitchView (at createNavigator.js:80)
    in Navigator (at createAppContainer.js:430)
    in NavigationContainer (at App.js:52)
    in NavContainer (at App.js:130)
    in ToastProvider (at App.js:129)
    in AccountProvider (at App.js:128)
    in RCTView (at ActionSheet/index.ios.tsx:13)
    in ActionSheet (at ActionSheetProvider.tsx:30)
    in ActionSheetProvider (at App.js:127)
    in LoadingProvider (at App.js:126)
    in Provider (at App.js:125)
    in App
    in RCTView (at AppContainer.js:101)
    in RCTView (at AppContainer.js:119)
    in AppContainer (at renderApplication.js:39)

MyDashboard
    MyDashboard.js:28:4
renderRoot
    [native code]:0
runRootCallback
    [native code]:0
unstable_runWithPriority
    scheduler.development.js:643:23
Component.prototype.setState
    react.development.js:325:31
NavigationContainer#dispatch
    createAppContainer.js:377:22
navigate
    navigationRef.js:9:24
<anonymous>
    AuthContext.js:207:4
tryCatch
    runtime.js:45:44
invoke
    runtime.js:271:30
tryCatch
    runtime.js:45:44
invoke
    runtime.js:135:28
Promise$argument_0
    runtime.js:170:17
tryCallTwo
    core.js:45:7
doResolve
    core.js:200:23
Promise
    core.js:66:12
Promise$argument_0
    runtime.js:169:27
enqueue
    runtime.js:192:38
exports.async
    runtime.js:216:8
<anonymous>
    AuthContext.js:203:2
getAuth.then$argument_0
    App.js:81:41
tryCallOne
    core.js:37:14
setImmediate$argument_0
    core.js:123:25
_callTimer
    JSTimers.js:146:14
_callImmediatesPass
    JSTimers.js:194:17
callImmediates
    JSTimers.js:458:30
callImmediates
    [native code]:0
flushedQueue
    [native code]:0
invokeCallbackAndReturnFlushedQueue
    [native code]:0

My imports:我的进口:

import React, { useContext, useEffect, useState } from 'react';
import AccountContext from '../../context/AccountContext';
import LoadingContext from '../../context/LoadingContext';
import { Context as AuthContext } from '../../context/AuthContext';
import {
  StyleSheet,
  SafeAreaView,
  View,
  Text,
  TouchableOpacity,
  ScrollView,
  Image,
  Alert,
  Dimensions,
} from 'react-native';
import colors from '../../utils/colors';
import globalStyles from '../../utils/globalStyles';
import api from '../../api/apiHelpers';


const MyDashboard = ({ navigation }) => {
  const { showLoading } = useContext(LoadingContext);
  const { getData } = useContext(AccountContext);
  const { state } = useContext(AuthContext);

  return (
      <SafeAreaView style={styles.container}>
        <ScrollView
          contextContainerStyles={{ flexGrow: 1}}
          showsVerticalScrollIndicator={false}>
          <Text>
            MyDashboard
          </Text>
          <Text>
            Welcome, {getData.name}
          </Text>
        </ScrollView>
      </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
});

export default MyDashboard;

I had the same problem, two days ago.前两天我也遇到了同样的问题。 I tried to reinstall or update the libraries and update react, reading some similar problems I read something about a bug in the react version, and solved my problem, but after installing a new library, this problem came back and my application still has the same problem我尝试重新安装或更新库并更新反应,阅读了一些类似的问题我阅读了有关反应版本中的错误的一些内容,并解决了我的问题,但是在安装新库后,这个问题又回来了,我的应用程序仍然如此问题

Bug article: https://github.com/facebook/react/issues/14474错误文章: https : //github.com/facebook/react/issues/14474

Try to check getData before or assign to empty object like this:尝试之前检查 getData 或分配给空对象,如下所示:

const MyDashboard = ({ navigation }) => {
  const { showLoading } = useContext(LoadingContext);
  const { getData } = useContext(AccountContext);
  const { state } = useContext(AuthContext);

  if(!getData) return null // <- check this

  return (
      <SafeAreaView style={styles.container}>
        <ScrollView
          contextContainerStyles={{ flexGrow: 1}}
          showsVerticalScrollIndicator={false}>
          <Text>
            MyDashboard
          </Text>
          <Text>
            Welcome, {getData.name}
          </Text>
        </ScrollView>
      </SafeAreaView>
  );
};

Hope it can help希望它可以帮助

请试试这个

import {AccountContext} from '../../context/AccountContext';

import {LoadingContext} from '../../context/LoadingContext';

暂无
暂无

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

相关问题 React Native Context - TypeError: undefined is not an object(评估“_useContext.theme”) - React Native Context - TypeError: undefined is not an object (evaluating '_useContext.theme') React-Native - 类型错误:未定义不是 object 评估 useContext - React-Native - TypeError: undefined is not an object evaluating useContext React Native 组件异常:未定义不是 object(evalating_useContext.isAuthenticated - React Native Component Exception: undefined is not an object (evaluating_useContext.isAuthenticated 类型错误:未定义不是对象(评估“_useContext.user”) - TypeError: undefined is not an object (evaluating '_useContext.user') React Native Expo with Moti 给出 TypeError: null is not an object (evaluating 'dispatcher.useContext') 错误 - React Native Expo with Moti gives TypeError: null is not an object (evaluating 'dispatcher.useContext') error TypeError:undefined不是对象(评估&#39;this.props&#39;) - React Native - TypeError: undefined is not an object (evaluating 'this.props') - React Native TypeError: undefined is not an object (evalating '_this.props') in my react native project - TypeError: undefined is not an object (evaluating '_this.props') in my react native project React-Native:TypeError:undefined 不是对象(评估“_this.props.item”) - React-Native: TypeError: undefined is not an object (evaluating '_this.props.item') TypeError:undefined 不是 React Native 中的对象(正在评估“addTodo(title, description).then”) - TypeError: undefined is not an object (evaluating 'addTodo(title, description).then') in React Native React Native:TypeError:undefined 不是一个对象(评估&#39;_this.props.data.map&#39;) - React Native: TypeError: undefined is not an object (evaluating '_this.props.data.map')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM