【问题标题】:Combine styles passed down as props组合作为道具传下来的样式
【发布时间】:2020-02-02 18:25:26
【问题描述】:

如何将我的样式作为道具传递下去?使用以下代码不显示样式道具:

容器:

<StyledText
          text="Episode Overview"
          backgroundStyle={[styles.opacityBackground, { margin: 0 }]}
        />

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: "column"
  },
  opacityBackground: { opacity: 0.8, backgroundColor: "#003333" },

});

组件

const StyledText = props => {
        let { text, backgroundStyle } = props;

        return (
            <View style={[styles.container, { ...backgroundStyle }]}>
                ...
            </View>
        );
    };

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    您应该将样式作为对象而不是数组传递

     <StyledText
          text="Episode Overview"
          backgroundStyle={{...styles.opacityBackground, margin: 0 }}
        />
    
    
    
    const StyledText = props => {
        let { text, backgroundStyle } = props;
    
        return (
            <View style={{...styles.container, ...backgroundStyle }}>
                ...
            </View>
        );
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-04
      • 2020-07-28
      • 2017-08-17
      • 2020-02-14
      • 2018-02-03
      • 2020-05-06
      • 2021-11-02
      • 2021-12-14
      相关资源
      最近更新 更多