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.

Styled Components does not allow me to set a background color

See original GitHub issue

So, just found out about Styled Components. I go on and yarn add it to my react native project, set it up and this happends:

Invariant Violation: Invalid prop `backgroundColor` supplied to `StyleSheet generated`: '#166191'
Valid color formats are
  - '#f0f' (#rgb)
  - '#f0fc' (#rgba)
  - '#ff00ff' (#rrggbb)
  - '#ff00ff00' (#rrggbbaa)
  - 'rgb(255, 255, 255)'
  - 'rgba(255, 255, 255, 1.0)'
  - 'hsl(360, 100%, 100%)'
  - 'hsla(360, 100%, 100%, 1.0)'
  - 'transparent'
  - 'red'
  - 0xff00ff00 (0xrrggbbaa)

StyleSheet generated: {
  "flexGrow": 1,
  "flexShrink": 1,
  "flexBasis": 0,
  "backgroundColor": "'#166191'"
}

Here is my common.js file to group the few common styles in my app

import styled from 'styled-components/native';

export const Container = styled.View`
    flex: 1;
    background-color: '#166191';
`;

On my component I import the container through import { Container } from '../styles/common'; and use it as <Container>. Why is this not working 😢

(moving all the code inside of the component file does not change anything)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

45reactions
gribnoysupcommented, Jan 2, 2018

@Leisors have you tried without quotes around the color value?

export const Container = styled.View`
    flex: 1;
    background-color: #166191;
`;
0reactions
alessandrocapracommented, May 11, 2020

@Leisors have you tried without quotes around the color value?

export const Container = styled.View`
    flex: 1;
    background-color: #166191;
`;

It finally worked, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Styles are not working in through styled components
I am trying to add background-color through ...
Read more >
FAQs - styled-components
Commonly asked questions about styled-components.
Read more >
How To Use React Styled Components Efficiently - Copycat
Learn how to set up organized React styled components in your project through examples, comprehensive explanations, and tips and tricks!
Read more >
Styled Components in React and TypeScript | by Michael Gold
The div's background color is assigned a value according to the ... Now let's install styled-components so we can do the styling using...
Read more >
Styled components, the styling library for your React apps you ...
We can simply install styled-components via NPM, like so: ... const Button = styled.button` background: black; color: white; border-radius: 7px; ...
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