StyleSheet - 在 React Native 中扩展样式值

Fab*_*hor 2 css stylesheet react-native

存在某种方法可以做这样的事情吗?

const styles = StyleSheet.create({
  content: {
    alignItems: 'center',
    flex: 1,
    justifyContent: 'center'
  },
  mainColor: { color: '#FFFFFF' },
  usernameIcon: {
    {this.styles.mainColor} // <-- How import the style, instead does write the code - color: '#FFFFFF'?
  },
  usernameItem: {
    backgroundColor: '#FF8484',
    borderColor: '#FFFFFF',
    paddingTop: 7
  },
});
Run Code Online (Sandbox Code Playgroud)

在我的组件中添加许多类,这非常冗长,我可能会做类似上面代码的事情。

riw*_*iwu 7

React Native 中没有样式继承语法(如 CSS 预处理器的语法)。

但既然都是 JS,你可以用 JS 来做:

const MAIN_COLOR = 'white';

const someStyle = {
  padding: 5,
  margin: 5,
}

const styles = StyleSheet.create({
  usernameIcon: {
    color: MAIN_COLOR,
    ...someStyle,
  },
  generalStyle: {
    backgroundColor: 'white',
  }
})

// you can also combine styles in the component
// note that the latter style will override duplicated styles
<View style={[styles.usernameIcon, styles.generalStyle]} />
Run Code Online (Sandbox Code Playgroud)