道具(样式)不会传递给孩子

时间:2020-05-29 16:01:49

标签: react-native components

我有这个组件

enter image description here

我正在尝试申请背景:

enter image description here

有人知道为什么不显示背景吗?

2 个答案:

答案 0 :(得分:1)

请将您的BorderLayout更改为以下格式。

 return (
    <View style={[styles.parent,props.style]}>
       /* remaining code */
    </View>
)

您不需要使用flatten,因为View会接受一组样式作为输入。通过以下方法,可以使用样式道具覆盖 BorderLayout 默认样式。

所做的更改: 将props.style移动到数组的末尾,以便它将覆盖默认样式(在您的情况下为backgroundColor)。

答案 1 :(得分:0)

在您的BorderLayout中,

return(
  <View style={props.style}>
    //rest implementation
  </View>
)

请确保您已从props.style中删除了StyleSheet.flatten(...)