question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Combining theming and styled-components

See original GitHub issue

When use react-native-elements theme provider and apply additional styles through styled-components it results in loosing styles defined in the theme.

I’m at 1.0.0-beta7 version and trying to combine theming (as top-level styling) and styled-components (for further tuning).

Explain what you did I have a setup that looks like:

Application index.js:

import React from 'react';
import { View } from  'react-native';
import { ThemeProvider, Text } from 'react-native-elements';
import { StyledText } from 'styled/Text/styled';
import theme from 'constants/theme';
...

class App extends React.Component {
  render(){
    return (
        <ThemeProvider theme={theme}>
           <View>
             <Text>Foo</Text>
             <StyledText>Bar</StyledText>
           </View>
        </ThemeProvider>
    );
  }
}

theme.js

import { Text } from 'react-native-elements';

export default {
  Text: {
    style: {
      color: 'red',
    }
  }
};

styled.js, which intent is to provide further styling for the Text component:

import styled from 'styled-components/native';

import { Text } from 'react-native-elements';

export const StyledText = styled(Text)`
  font-size: 16px;
`;

Expected behaviour I expect both Text and StyledText elements rendered with the red text color.

Describe the bug Only the Text component gets a red colored text Foo, StyledText doesn’t get red color.

I’m not honestly sure if it is a bug, but I can’t find the way for styled component to respect the theme.

Your Environment (please complete the following information):

software version
react-native-elements 1.0.0-beta7
styled-components 4.1.3
react-native https://github.com/expo/react-native/archive/sdk-30.0.0.tar.gz
yarn 1.7.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
arpitBhallacommented, Mar 10, 2022

Yupp, Could you create a PR for this?

Make changes in website/docs/*

3reactions
iRoachiecommented, Dec 22, 2018

This is a little tricky to fix on our end. The probably here is that styled-components ends up passing a style prop that is an array of objects.

Example:

export const StyledText = styled(Text)`
  font-size: 16px;
`

The above results in:

<Text style={[{ fontSize: 16 ]} />

The value in your theme results in:

<Text style={{ color: 'red' }} />

The theme withTheme hoc on the Text component tries to merge the incoming props, but the problem is that one is an array and the other is an object. Since these two can’t merge the latter is taken and that’s why you only see the font size being taken effect.

An easy way to fix this in userland is to change the value in your theme to an array:

// theme.js

import { Text } from 'react-native-elements';

export default {
  Text: {
    style: [{
      color: 'red',
    }]
  }
};

This should solve the problem, except for a proptypes warning

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to extend or merge Styled Components theme
1 Answer 1 ; import React ; import · from ; Title · CompLibThemeProvider, ; 'component-lib' · import { ; "styled-components" · const...
Read more >
Advanced Usage - styled-components
Theming. styled-components has full theming support by exporting a <ThemeProvider> wrapper component. This component provides a theme to all React components ...
Read more >
Build a React theme switcher app with styled-components
In this tutorial, we demonstrate how to build a custom theme switcher for a Game of Thrones-themed React app using styled-components.
Read more >
Merging themes more easily · Issue #244 · styled-components ...
Let's just merge arrays of themes the same way. That should be fine for the 95% use case, and if users do need...
Read more >
Theming with Styled Components | The Mindless
A guide on theming your web applications with Styled Components. Why should you theme? Themes help create an identity for your application.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found