开发者问题收集

TypeError:未定义不是一个对象(评估'_expoConstants.Constants.statusBarHeight')

2022-01-02
890

我正在尝试使用 Constants.statusBarHeight 将视图与 react-native 中的状态栏分开,我使用 expo install expo-constants

安装了 Constants,但是我收到此错误:

TypeError: undefined is not an object (evaluating '_expoConstants.Constants.statusBarHeight')
at node_modules\react-native\Libraries\LogBox\LogBox.js:149:8 in registerError
at node_modules\react-native\Libraries\LogBox\LogBox.js:60:8 in errorImpl
at node_modules\react-native\Libraries\LogBox\LogBox.js:34:4 in console.error
at node_modules\expo\build\environment\react-native-logs.fx.js:27:4 in error
at node_modules\react-native\Libraries\Core\ExceptionsManager.js:104:6 in reportException
at node_modules\react-native\Libraries\Core\ExceptionsManager.js:172:19 in handleException
at node_modules\react-native\Libraries\Core\setUpErrorHandling.js:24:6 in handleError
at node_modules\@react-native\polyfills\error-guard.js:49:36 in ErrorUtils.reportFatalError
at node_modules\metro-runtime\src\polyfills\require.js:204:6 in guardedLoadModule
at http://192.168.1.5:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false&minify=false:137612:3 in global code

这是我的源代码:

import React from "react";
import Constants from "expo-constants";
import { StyleSheet, SafeAreaView, View } from "react-native";

function Screen({ children, style }) {
  return (
    <SafeAreaView style={[styles.screen, style]}>
      <View style={style}>{children}</View>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  screen: {
    paddingTop: Constants.statusBarHeight,
    flex: 1,
  },
});

export default Screen;

似乎 Vs code 无法导入 Constants。

2个回答

似乎您已经导入了 expo 常量并以正确的方式安装了它,请尝试以下技巧,如果它有效,声明变量 const barHeight = Constants.statusBarHeight; 并在您的样式属性中使用

Comunev HR
2022-01-02

导入 expo-constants

import * as Constants from 'expo-constants'

然后使用以下样式:

const styles = StyleSheet.create({
screen: {
paddingTop: Constants.default.statusBarHeight,
flex: 1,
 },
});
arman amirkamali
2022-05-10