React Native全局样式

时间:2015-06-15 19:11:01

标签: react-native

我是React的新手,我了解基于组件的内联样式的好处。但我想知道是否有一种体面的方式来拥有某种全球风格。例如,我想在整个应用中使用相同的文字和按钮颜色。

不是在每个组件中重复(并且如果需要,随后必须在x个位置进行更改),我最初的想法是创建一个基础' StyleSheet类在它自己的文件中,并将其导入我的组件。

是否有更好或更多的反应'方式是什么?

11 个答案:

答案 0 :(得分:105)

您可以创建可重复使用的样式表。例如:

<强> style.js

'use strict';

var React = require('react-native');

var {
  StyleSheet,
} = React;

module.exports = StyleSheet.create({

alwaysred: {
    backgroundColor: 'red',
    height: 100,
    width: 100,
},

});

在您的组件中:

var s = require('./style');

...然后:

<View style={s.alwaysred} ></View>

答案 1 :(得分:66)

为您的样式创建一个文件(I.E.,Style.js)。

以下是一个例子:

import { StyleSheet } from 'react-native';

export default StyleSheet.create({
  container: {
    flex: 1
  },
  welcome: {
    fontSize: 20
  }
});

在您要使用样式的任何文件中,添加以下内容:

import styles from './Style'

答案 2 :(得分:9)

如果您只想设置一些全局变量,可以试试。

<强> AppStyles.js

export default AppStyles = {
    colour: {
        background: '#f4f9fd',
        font: '#434e5a',
        primary: '#ff3b30'
    }
}

<强> index.ios.js

import AppStyles from './AppStyles';

const styles = StyleSheet.create({
    container: {
        backgroundColor: AppStyles.colour.background
    }
});

答案 3 :(得分:8)

您也可以尝试支持全局样式变量的react-native-extended-stylesheet

// app.js
EStyleSheet.build({
   buttonColor: 'green'
});

// component.js
var styles = EStyleSheet.create({
  button: {
    backgroundColor: '$buttonColor',
    ...
  }
});

答案 4 :(得分:8)

您必须创建一个文件来存储其中的所有样式,如“ styles.js ”,并根据需要编写css类型代码

'use strict';
import {StyleSheet} from 'react-native';

export default StyleSheet.create({

    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },

    title: {
        fontSize: 10,
        color: '#000',
        backgroundColor: '#fff'
    },

    button: {
        fontSize: 12,
        color: '#000',
        backgroundColor: '#fff'
    }

});

现在您可以使用全局样式

import React, { Component } from 'react';
import { View, Button } from 'react-native';
import StyleSheet from './config/styles';

export default class App extends Component {

  render() {
    return (
      <View
        style={StyleSheet.container}>
        <Button
          style={StyleSheet.button}
          title="Go to Lucy's profile"
        />
      </View>
    );
  }
}

答案 5 :(得分:3)

试试我的库react-native-style-tachyons,它不仅为您提供全局样式,还提供了一个设计优先的响应式布局系统,其宽度和高度相对于您的根字体大小。

答案 6 :(得分:1)

外部CSS文件main.css

'use strict';

var {
   StyleSheet,
 } = 'react-native';

module.exports = StyleSheet.create({

  main: {
     backgroundColor: 'gray',
     height: 20,
     width: 100,
  }
});

在组件中创建css文件的实例。

var mainCss = require('./main');

<View style={mainCss.main}><Text>Hello</Text></View>

答案 7 :(得分:1)

所有这些方法都直接回答了问题,但这不是解决问题的方法。这些人陷入了旧的思维方式,即在何处设置可用元素的样式。在React中,您可以制作组件。因此,如果您需要一个不存在的基本组件,则可以使用它。例如:

function getStyle (styleProp) {
  return {
    color   : getTheme().text,
    fontSize: 14,
    ...styleProp
  }
}

export default function CustomText (props) {
  return (
    <Text style={getStyle(props.style)}>
      {props.children}
    </Text>
  )
}

,然后在各处使用CustomText而不是Text。您也可以使用Viewdivspan或其他任何方法来实现。

答案 8 :(得分:0)

在这里,您可以找到一种优雅的方式来对样式进行排序,然后将其导入到不同的组件中,可以创建一个文件夹,在其中收集所有样式文件,并创建和index.js,它将作为外观使用: / p>

index.js将如下所示:

import Variables from './variables';
import GlobalStyles from './global'; 

export { Variables, GlobalStyles };

然后像这样导入:

import { GlobalStyles } from './stylesheets/index';

此处提供更多信息:

https://thoughtbot.com/blog/structure-for-styling-in-react-native

答案 9 :(得分:0)

我使用类似的软件

创建一个名为“ constants”的目录 在./constants/AppStyles.js

中创建文件
karate-base.js

然后在App.js中引用此文件和创建的样式。

 /**
 * Project level stylesheet for common styles
 */


import { StyleSheet } from 'react-native';

export default StyleSheet.create({
  container: {
    flex: 1,
    margin: 20,
    alignItems: 'center',
    alignContent: 'center',
    paddingVertical: 60
  }
  
});

我掉进去的陷阱

  • 我从'./constants/AppStyles'导入{AppStyles}时花括号;错误:-(
  • 我创建了AppStyles作为组件,并尝试导出const WRONG:-)

在网上找到了一门不错的课程,并从那里弄清楚了

答案 10 :(得分:-2)

查看Shoutem Themes的React Native。

以下是Shoutem主题的内容:

某种样式通过其样式名称自动应用于组件的全局样式:

const theme = {
  'my.app.ComponentStyleName': {
    backgroundColor: 'navy',
  },
};

使用styleName激活某些特定于组件的样式(如CSS类):

const theme = {
  'my.app.ComponentStyleName': {
    '.rounded': {
      borderRadius: 20,
    },
    backgroundColor: 'navy',
  },
};

// Implementation - will apply borderRadius to Component
<Component styleName="rounded"/>

自动样式继承:

const theme = {
  'my.app.ComponentStyleName': {
    'my.app.ChildComponentStyleName': {
      backgroundColor: 'red',
    },
    '.rounded': {
      borderRadius: 20,
    },
    backgroundColor: 'navy',
  },
};

// Implementation - will apply red background color to ChildComponent
<Component styleName="rounded">
  <ChildComponent/>
</Component>

组合组件的嵌套样式:

const theme = {
  'my.app.ComponentStyleName': {
    containerView: {
      paddingTop: 10,
    },
    innerView: {
      backgroundColor: 'yellow',
    },
  },
};

// Of course do not forget to connect Component
function Component({ style }) {
  return (
   <View style={style.containerView}>
    <View style={style.innerView}>
     <Text>Warning with yellow background.</Text>
    </View>
   </View>
  );
}

要使其工作,您需要使用StyleProvider,这是一个包装器组件,它通过上下文为所有其他组件提供样式。与Redux StoreProvider类似。

此外,您需要使用connectStyle将组件连接到样式,以便始终使用连接组件。与Redux connect类似。

export const styledComponent = connectStyle('my.app.ComponentStyleName',
                                { ...defaultStyleIfAny })(Component);

您可以在文档中看到示例。

最后一点,我们还在UI ToolKit中提供了一组已连接到样式的组件,因此您只需在全局样式/主题中导入它们并设置样式。

相关问题