将颜色变量导入我的样式

ren*_*dom 8 react-native

我想用我的颜色变量创建文件,然后将此文件导入我的样式.

button: {
 color: primarycolor
 background: primarybackground
}
Run Code Online (Sandbox Code Playgroud)

所以我只能在一个地方改变颜色.

Ani*_*vle 16

您可以定义一个单独的js文件,该文件由colors对象组成并导出它.

const COLORS = {
  white: '#fff',
  black: '#000',
  // your colors
}

export default COLORS;
Run Code Online (Sandbox Code Playgroud)

现在您使用定义颜色的相应文件中的上述文件导入颜色.并使用它如下所示.

button: {
  color: COLORS.white,
  backgroundColor: COLORS.black
}
Run Code Online (Sandbox Code Playgroud)


Nou*_*far 5

有点晚了。但是一个完整的例子。创建一个常量,如

const Colors {
  tabIconDefault: '#959292',
  tabIconSelected: tintColor,
  tabBar: '#fefefe',
  errorBackground: 'red',
  errorText: '#fff',
  warningBackground: '#EAEB5E',
  warningText: '#666804',
  noticeBackground: tintColor,
  noticeText: '#fff'
};
module.exports = Colors ;
Run Code Online (Sandbox Code Playgroud)

要在任何地方使用,请像这样导入

const Colors = '../constants/Colors';
Run Code Online (Sandbox Code Playgroud)

现在使用它就像

color: Colors.warningBackground
Run Code Online (Sandbox Code Playgroud)

希望有帮助。