【发布时间】:2018-10-20 01:36:09
【问题描述】:
我试图让我的组件使用样式组件在 react-native 中使用动态样式。此处显示了执行此操作的方法https://github.com/styled-components/styled-components/issues/940
const ColorAnimation = styled.div.attrs({
style: props => ({
color: props.color
})
})`
// static styles
它也适用于 React Native,如此处所示。 https://snack.expo.io/ryIXsAe0M
import React, { Component } from 'react';
import { View } from 'react-native';
import styled from 'styled-components/native'; // 2.2.4
const StyledView = styled.View.attrs({
style: props => ({
backgroundColor: props.backgroundColor,
height: props.height,
}),
})`
background-color: papayawhip;
`;
const StyledText = styled.Text`
color: palevioletred;
`;
const RotatedBox = styled.View`
transform: rotate(90deg);
text-shadow-offset: 10px 5px;
font-variant: small-caps;
margin: 5px 7px 2px;
`;
export default class App extends Component {
render() {
return (
<View style={{ marginTop: 50 }}>
<StyledView height={100} backgroundColor="yellow">
<StyledText>Hello World!</StyledText>
</StyledView>
<RotatedBox />
</View>
);
}
}
但是,我宁愿只传入一个 customStyle 道具,其中包含我想要使用的所有动态样式。像这样。 . . https://snack.expo.io/BkpToRe0f
const StyledView = styled.View.attrs({
style: props => props.customStyles,
})`
background-color: papayawhip;
`;
<StyledView customStyles={{ height: 100, backgroundColor: 'yelllow' }}>
很遗憾,这不适用于样式。有人知道为什么吗?另外是否有替代解决方案?
【问题讨论】:
标签: react-native styled-components