简体   繁体   English

在本机反应中导出c​​onst变量undefined

[英]export const variable undefined on react native

I have defined some const on Color.js, but some of the Color is undefined when i call it on Screen.js 我在Color.js上定义了一些const,但是当我在Screen.js上调用它时,一些Color是未定义的

I use react-native 0.45 for this project. 我在本项目中使用react-native 0.45。 Here's the code i wrote : 这是我写的代码:

Color.js Color.js

export const ATHENS_GRAY = '#EDEEF0';
export const AQUA_SPRING = '#F8FBFD';
export const BLACK = '#000000';
export const BRIGHT_TURQUOISE = '#1BC1F1';
export const CATSKILL_WHITE = '#E4ECF4';
export const FROLY = '#F68181';
export const FUN_BLUE = '#1B61AD';
export const HIT_GRAY = '#A3AEB9';
export const JUMBO = '#7C7D80';
export const LIMED_SPRUCE = '#3D474C';

Screen.js Screen.js

import React, {Component} from 'react';
import { View, Text, FlatList, StyleSheet } from 'react-native';
import * as Color from './Color';

export default class Screen extends Component {
  constructor(props) {
    super(props);
    console.log(Color.BRIGHT_TURQUOISE);
    console.log(Color.FUN_BLUE);
  }

  render() {
    return (<View/>)
  }
}

The result of the console.log : console.log的结果:

Color.BRIGHT_TURQUOISE is undefined Color.BRIGHT_TURQUOISE未定义

Color.FUN_BLUE is '#1B61AD' Color.FUN_BLUE是'#1B61AD'

Do you have any idea to solve this problem? 你有什么想法解决这个问题吗?

I have try it on my terminal using react-native log-android , and both values ​​are obtained like this : 我已经使用react-native log-android在我的终端上尝试了,并且这两个值都是这样获得的:

enter image description here 在此输入图像描述

Or maybe you can try another way to export your constant on your Color.js like this : 或者你可以尝试另一种方法在你的Color.js上导出你的常量,如下所示:

module.exports = Object.freeze({
   ATHENS_GRAY : '#EDEEF0',
   AQUA_SPRING : '#F8FBFD',
   BLACK : '#000000',
   BRIGHT_TURQUOISE : '#1BC1F1',
   CATSKILL_WHITE : '#E4ECF4',
   FROLY : '#F68181',
   FUN_BLUE : '#1B61AD',
   HIT_GRAY : '#A3AEB9',
   JUMBO : '#7C7D80',
   LIMED_SPRUCE : '#3D474C',
});

And you can try again :) 你可以再试一次:)

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

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