我为自己缺乏知识而道歉.我想在文件中导入常量值.我家里有两份文件.js和风格.js在同一个目录中.

家js

import React from 'react';
import styles from './styles';

const Home = (props) => {

    const HEADER_MAX_HEIGHT = 200;

}

export { HEADER_MAX_HEIGHT };
export default Home;

而且风格各异.js

import { StyleSheet } from 'react-native'
import { HEADER_MAX_HEIGHT } from './Home';

export default StyleSheet.create({
    header: {
        height: HEADER_MAX_HEIGHT
    }
});

但我得到了这个错误

找不到变量:页眉最大高度

如何在样式中访问该变量.js?

推荐答案

try :

家js

import React from 'react';
import styles from './styles';

export const HEADER_MAX_HEIGHT = 200;

const Home = props => <h1>Home</h1>;

export default Home;

风格.js

import { StyleSheet } from 'react-native';
import { HEADER_MAX_HEIGHT } from './Home';

export default StyleSheet.create({
  header: {
    height: HEADER_MAX_HEIGHT,
  },
});

Your HEADER_MAX_HEIGHT needs to be within the 家js file but outside of the Home Component. You can read about it here: Javascript Scope

React-native相关问答推荐

错误:HostBody::get for prop NativeUnimoduleProxy中出现异常- Android虚拟设备(AVD)使用Expo测试React Native App时崩溃

类似Snapchat的Reaction Native筛选器

TextInput 防止 ScrollView 滚动

React Native:约束 Animated.Value

ReactNative 0.43-rc.2 FlatList -- 试图获取超出范围索引 0 的框架

React Native 上传到 S3 存储空间

如何在 react-native 中在图像周围添加阴影

React Native with Typescript and Jest 在 0.57 更新后被 destruct :Couldn't find preset "module:metro-react-native-babel-preset" relative to directory

React Native 使视图 Hug键盘顶部

mapDispatchToProps:有什么意义吗?

无法在 react native >= 0.60 中链接assets(字体)

React Native Negative shadowOffset 创建顶部阴影

未计算文件的 React-Native Bundle Error 错误 SHA-1

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.

[React-Native][Jest]SyntaxError: Unexpected token import

由于对 sourcetree 的 husky 预推送,Git 推送失败

Tools > Android 菜单在 Android Studio 中不存在

如何将 jest 测试移动到 /test 文件夹并让它们运行?

React Native,更改 React Navigation 标题样式

React Native <>(空)组件到底是什么