12

我尝试为其创建一个具有特定样式的按钮。我有超过 3 个属性,例如 justifyContent、alignItems、backgroundColor 和 height。我想将另一个组件的样式传递给它,以便按钮的 backgroundColor 属性发生变化。

我的代码:

import React from 'react';
import { Text, TouchableOpacity } from 'react-native';

const Button = ({ buttonName, csCode }) => {
  const { buttonStyle, textStyle } = styles;

  return (
    <TouchableOpacity style={{ buttonStyle, backgroundColor: [csCode] }}>
      <Text style={textStyle}>
      {buttonName}
      </Text>
    </TouchableOpacity>
  );
};

const styles = {
  textStyle: {
    alignSelf: 'center',
    color: '#ffffff',
    fontSize: 35,
    fontWeight: '600',

  },
  buttonStyle: {
    justifyContent: 'center',
    alignSelf: 'stretch',
    height: '20%',
  }
};

export { Button };

在这里,buttonStyle 没有应用到按钮上,而是只应用了 backgroundColor 属性。有什么帮助吗?

谢谢。

4

1 回答 1

13

如果您想同时使用样式对象中的样式和内联样式,请将它们放在一个数组中,如下所示:

<TouchableOpacity style={[buttonStyle, {backgroundColor: csCode }]}>
  ...
</TouchableOpacity>
于 2018-01-06T21:47:52.727 回答