【发布时间】:2021-04-23 12:09:56
【问题描述】:
我想创建一个可以将道具传递给样式表的自定义按钮组件。在这个项目中,我要发送的道具是十六进制代码,因此组件可以根据传递给组件的颜色道具具有不同的颜色。所以,我写了这个
type Props = {
backgroundColor: string;
borderColor: string;
};
export const CustomButton = ({
backgroundColor,
borderColor,
}: Props) => {
return (
<TouchableOpacity
style={styles({ backgroundColor, borderColor }).container}
>
</TouchableOpacity>
);
const styles = ({ backgroundColor, borderColor }: Props) =>
StyleSheet.create({
container: {
backgroundColor: backgroundColor,
borderColor: borderColor,
},
});
思路是将props传递给组件,然后传递给Touchable组件上的样式。但是,这行不通。这总是在styles({ backgroundColor, borderColor}).container 上给我这个错误
Argument of type "{ backgroundColor, borderColor }" is not assignable to parameter of type "Props".
那么,出了什么问题?我应该如何将道具传递给样式表?
【问题讨论】: