3

I have been using Styled-components with React web for a while now, but recently I have started working on a React Native app which I decided to use styled-compoents in. It's been great when styling components that have just the style property, such as the default react-native components.

The problem I've been having though is when I need to style a more complex component that has multiple style properties such as containerStyle, inputStyle, and others.

When it's only one style property with a different name I can do the following:

const StyledBadge = styled(({ style, ...rest }) => {
  return <Badge {...rest} containerStyle={style} />;
})`
  position: absolute;
  right: 0;
  top: 0;
`;

This works flawlessly but when the component has multiple styles, I have no idea what to do:

const StyledList = styled(({ style, ...rest }) => {
  return <List {...rest} containerStyle={style} searchInputStyle={?} searchItemStyle={?} />;
})`
`;

With components like Gifted-React-Native-Chat is even worse because it has properties for passing properties as objects to its internal components, such as messageProps, listViewProps, containerProps and all of them have the style property.

Does anyone have any idea how to do that or if it's even possible?

I've been searching and trying to find a solution for this for a few days but I can't.

Thanks!

5
  • I'm also curious about this as well for React Native. Did you find a solution? Commented Mar 2, 2019 at 18:43
  • Hey @Larbear, No I haven't =/ Commented Mar 11, 2019 at 3:25
  • You have to use .attrs constructor mentioned here styled-components.com/docs/basics#attaching-additional-props but will lose the css syntax rules and have to use styles like you would with Stylesheet. Commented Mar 12, 2019 at 6:05
  • Thanks @Larbear, That's pretty awful but it gave me a really good idea on how to solve this. I'll leave an update here once I have it solved. Commented Mar 13, 2019 at 3:59
  • 1
    @Eric.M How did it go? Any update? Commented Jul 31, 2019 at 5:10

1 Answer 1

2

Here's how we ended up doing it.

Styled-components only work with the style prop but many custom components don't expose this prop. Instead they provide a *Style prop that gets passed to child component style props.

As an example, react-native-material-textfield has 5 style props. enter image description here

We use the attrs function to keep the organization of styles in one file with the rest of the styled components.

styled-component attrs example

This doesn't allow you to use traditional css syntax for the pseudo component, but it's the best we could think of to keep all styles organized.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.