React native 不同组件的不同样式文件

React native 不同组件的不同样式文件,react-native,React Native,我正在学习反应本族语,并提出了一个问题。我正在关注一系列教程,但可能错过了一些东西。我正在尝试为不同的组件使用不同的样式文件,但这些样式似乎不适用。 以下是代码片段: index.ios.js import React, { AppRegistry, Component, Text, View, StatusBar } from 'react-native'; StatusBar.setBarStyle('light-content'); class iTunesBrow

我正在学习反应本族语,并提出了一个问题。我正在关注一系列教程,但可能错过了一些东西。我正在尝试为不同的组件使用不同的样式文件,但这些样式似乎不适用。
以下是代码片段:

index.ios.js

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

StatusBar.setBarStyle('light-content');

class iTunesBrowser extends Component {
  render() {
    return (
      <View style={styles.global.mainContainer} >
        <View style={styles.navbar.appearance}>
        <View style={styles.navbar.button} ></View>
          <Text style={styles.navbar.title}>iTunesBrowser</Text>
          <Text style={styles.navbar.button} >Search</Text>
        </View>

        <View style={styles.global.content}>
          <Text> Some Text </Text>
        </View>
      </View>
    );
  }
}

import styles from './styles';

AppRegistry.registerComponent('iTunesBrowser', () => iTunesBrowser);
export default {
    global: require('./styles/global'),
    navbar: require('./styles/navbar')
};
import React, {StyleSheet} from 'react-native';

export default StyleSheet.create({
  mainContainer: {
    flex: 1
  },
  content: {
    flex: 1,
    backgroundColor: '#ccc'
  }
});
import React, {StyleSheet} from 'react-native';

export default StyleSheet.create({
  appearance: {
    backgroundColor: '#003333',
    paddingTop: 30,
    paddingBottom: 10,
    flexDirection: 'row'
  },
  title: {
    color: '#FFFFFF',
    textAlign: 'center',
    fontWeight: 'bold',
    flex: 1
  },
  button: {
    width: 50,
    color: '#FEFEFE',
    textAlign: 'center'
  }
});
style/global.js

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

StatusBar.setBarStyle('light-content');

class iTunesBrowser extends Component {
  render() {
    return (
      <View style={styles.global.mainContainer} >
        <View style={styles.navbar.appearance}>
        <View style={styles.navbar.button} ></View>
          <Text style={styles.navbar.title}>iTunesBrowser</Text>
          <Text style={styles.navbar.button} >Search</Text>
        </View>

        <View style={styles.global.content}>
          <Text> Some Text </Text>
        </View>
      </View>
    );
  }
}

import styles from './styles';

AppRegistry.registerComponent('iTunesBrowser', () => iTunesBrowser);
export default {
    global: require('./styles/global'),
    navbar: require('./styles/navbar')
};
import React, {StyleSheet} from 'react-native';

export default StyleSheet.create({
  mainContainer: {
    flex: 1
  },
  content: {
    flex: 1,
    backgroundColor: '#ccc'
  }
});
import React, {StyleSheet} from 'react-native';

export default StyleSheet.create({
  appearance: {
    backgroundColor: '#003333',
    paddingTop: 30,
    paddingBottom: 10,
    flexDirection: 'row'
  },
  title: {
    color: '#FFFFFF',
    textAlign: 'center',
    fontWeight: 'bold',
    flex: 1
  },
  button: {
    width: 50,
    color: '#FEFEFE',
    textAlign: 'center'
  }
});
样式/navbar.js

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

StatusBar.setBarStyle('light-content');

class iTunesBrowser extends Component {
  render() {
    return (
      <View style={styles.global.mainContainer} >
        <View style={styles.navbar.appearance}>
        <View style={styles.navbar.button} ></View>
          <Text style={styles.navbar.title}>iTunesBrowser</Text>
          <Text style={styles.navbar.button} >Search</Text>
        </View>

        <View style={styles.global.content}>
          <Text> Some Text </Text>
        </View>
      </View>
    );
  }
}

import styles from './styles';

AppRegistry.registerComponent('iTunesBrowser', () => iTunesBrowser);
export default {
    global: require('./styles/global'),
    navbar: require('./styles/navbar')
};
import React, {StyleSheet} from 'react-native';

export default StyleSheet.create({
  mainContainer: {
    flex: 1
  },
  content: {
    flex: 1,
    backgroundColor: '#ccc'
  }
});
import React, {StyleSheet} from 'react-native';

export default StyleSheet.create({
  appearance: {
    backgroundColor: '#003333',
    paddingTop: 30,
    paddingBottom: 10,
    flexDirection: 'row'
  },
  title: {
    color: '#FFFFFF',
    textAlign: 'center',
    fontWeight: 'bold',
    flex: 1
  },
  button: {
    width: 50,
    color: '#FEFEFE',
    textAlign: 'center'
  }
});

将样式放在索引单个样式文件中时,样式才起作用。

在React Native中使用组件样式的典型方法是将样式表的创建保留在组件本身中,而不是像您在web上使用CSS时所使用的那样,保留在单独的文件中。这样,有关组件、结构和样式的所有内容都在同一个文件中

您仍然可以拥有一个全局样式表,并在需要时引入这些样式,但在组件中保留组件样式


关于社区和Facebook如何建议您设置组件样式的一个很好的例子,请查看:

在React Native中使用组件样式的典型方法是将样式表的创建保存在组件本身中,而不是像您在web上使用CSS时那样保存在单独的文件中。这样,有关组件、结构和样式的所有内容都在同一个文件中

您仍然可以拥有一个全局样式表,并在需要时引入这些样式,但在组件中保留组件样式


有关社区和Facebook如何推荐您的组件样式的一个很好的示例,请查看:

我已经尝试了您的示例,当您使用JS文件时,它会起作用:

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

var global = StyleSheet.create({
    mainContainer: {
        flex: 1
    },
    content: {
        flex: 1,
        backgroundColor: '#ccc'
    }
});

module.exports = global;


我已经尝试过您的示例,当您使用JS文件时,它会起作用:

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

var global = StyleSheet.create({
    mainContainer: {
        flex: 1
    },
    content: {
        flex: 1,
        backgroundColor: '#ccc'
    }
});

module.exports = global;