I'm trying to have my component use dynamic styles in react-native using styled-components. The way to do this is shown here https://github.com/styled-components/styled-components/issues/940
const ColorAnimation = styled.div.attrs({
style: props => ({
color: props.color
})
})`
// static styles
It works in React Native as well, as shown here. 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>
);
}
}
However, I'd rather just pass in a customStyle prop that contains all the dynamic styles I want to use. Like this. . . https://snack.expo.io/BkpToRe0f
const StyledView = styled.View.attrs({
style: props => props.customStyles,
})`
background-color: papayawhip;
`;
<StyledView customStyles={{ height: 100, backgroundColor: 'yelllow' }}>
Unfortunately, this does not apply the styles. Does anyone know why this is? Also if there is an alternative solution?