小编Ami*_*rth的帖子

如何在react-native中传递特定组件的样式道具

我尝试创建一个具有特定样式的按钮.我有超过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 };
Run Code Online (Sandbox Code Playgroud)

这里,buttonStyle不应用于按钮,而只应用backgroundColor prop.有帮助吗?

谢谢.

styles reactjs react-native react-props

9
推荐指数
1
解决办法
1万
查看次数

标签 统计

react-native ×1

react-props ×1

reactjs ×1

styles ×1