react-native - react native : how to import flexbox styles from another file js?

标签 react-native flexbox

我是 React Native 的新手,我尝试从 tutorial point 学习基本的 Flexbox但我总是收到错误,我有两个文件index.android.js和MyPresentationalComponent.js用于样式代码文件。

This error picture, when I run myproject

index.android.js

import React, { Component } from 'react';
import {
  AppRegistry,
  styles,
  View
} from 'react-native';

import MyPresentationalComponent from './MyPresentationalComponent';

export default class belajar extends Component {
   constructor() {
      super();
      this.state = {
         myText: 'Lorem ipsum dolor sit amet.'
      };
   }
   render() {
      return (
         <View>
                <MyPresentationalComponent style={styles.container} />
         </View>
      );
   }
}
AppRegistry.registerComponent('belajar', () => belajar);

MyPresentationalComponent.js

import React, { Component } from 'react';

import {
   View,
   StyleSheet
} from 'react-native';

const MyPresentationalComponent = (props) => {
   return (
      <View style={styles.container}>
         <View style={styles.redbox} />
         <View style={styles.bluebox} />
         <View style={styles.blackbox} />
      </View>
   );
};

export default MyPresentationalComponent;

const styles = StyleSheet.create({
   container: {
      flex: 1,
      flexDirection: 'column',
      justifyContent: 'center',
      alignItems: 'center',
      backgroundColor: 'grey',
      height: 600
   },
   redbox: {
      width: 100,
      height: 100,
      backgroundColor: 'red'
   },
   bluebox: {
      width: 100,
      height: 100,
      backgroundColor: 'blue'
   },
   blackbox: {
      width: 100,
      height: 100,
      backgroundColor: 'black'
   },
});

最佳答案

问题是您没有从文件中导出 styles 变量,因此即使您导入标有 export default 的类,其他文件也看不到它。我建议如下:

import React, { Component } from 'react';

import {
   View,
   StyleSheet
} from 'react-native';

const MyPresentationalComponent = (props) => {
   return (
      <View style={styles.container}>
         <View style={styles.redbox} />
         <View style={styles.bluebox} />
         <View style={styles.blackbox} />
      </View>
   );
};

export default MyPresentationalComponent;

export const styles = StyleSheet.create({
   container: {
      flex: 1,
      flexDirection: 'column',
      justifyContent: 'center',
      alignItems: 'center',
      backgroundColor: 'grey',
      height: 600
   },
   redbox: {
      width: 100,
      height: 100,
      backgroundColor: 'red'
   },
   bluebox: {
      width: 100,
      height: 100,
      backgroundColor: 'blue'
   },
   blackbox: {
      width: 100,
      height: 100,
      backgroundColor: 'black'
   },
});

然后,您的index.android.js 应该如下所示:

import React, { Component } from 'react';
import {
  AppRegistry,
  styles,
  View
} from 'react-native';

import MyPresentationalComponent, {styles} from './MyPresentationalComponent';

export default class belajar extends Component {
   constructor() {
      super();
      this.state = {
         myText: 'Lorem ipsum dolor sit amet.'
      };
   }
   render() {
      return (
         <View>
                <MyPresentationalComponent style={styles.container} />
         </View>
      );
   }
}
AppRegistry.registerComponent('belajar', () => belajar);

请注意默认导出和命名导出之间的区别。您可以只有一个默认导出,并且您为它指定的名称根本不相关,当您导入它时,它可以使用不同的名称。对于命名导入,您需要使用花括号表示法,并且 exportimport 中的名称必须相同。但是,您可以拥有任意数量的它们。

关于react-native - react native : how to import flexbox styles from another file js?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41661705/

相关文章:

javascript - 获取 SectionList 项的索引 React-Native

css - 为什么 Flex 和 max-height vh 工作不正确?

html - 将按钮设置为蓝色条的大小?

reactjs - react-native-navigation如何检测 `shouldComponentUpdate`内的当前屏幕

ios - 如何在 React Native 中的 Google map 上实现可拖动、可调整大小的多边形?

html - 溢出属性在 IE 11 中不起作用

html - 当元素之一比另一个元素高 2 倍时,css3 flex 对齐

javascript - 防止 flex 元素中的文本区域和镜像内容随内容动态扩展

reactjs - React Native - 无法创建文本输入

ios - React navigation navigation.navigate 在使用相机时不会转到下一个屏幕