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.

Unable to add theme to any Styled component for even basic usage

See original GitHub issue

I tried basic implementation of Theming Styled components, as shown in the documentation Styled component Theming

I tried with my basic parent component,

const theme = {
	main: 'mediumseagreen'
};
const Button = styled.button`
	font-size: 1em;
	margin: 1em;
	padding: 0.25em 1em;
	border-radius: 3px;

	/* Color the border and text with theme.main */
	color: ${props => props.theme.main};
	border: 2px solid ${props => props.theme.main};
`;
ReactDOM.render(
	<ThemeProvider theme={theme}>
		<Button>Themed</Button>
	</ThemeProvider>,
	document.getElementById('root')
);

build passes but browser consoles an Error

**Uncaught Error: Cannot create styled-component for component: [object Object]**
    at constructWithOptions (styled-components.es.js:2089)
    at styled (styled-components.es.js:2024)
    at mountIndeterminateComponent (react-dom.development.js:8038)
    at beginWork (react-dom.development.js:8229)
    at performUnitOfWork (react-dom.development.js:10215)
    at workLoop (react-dom.development.js:10279)
    at HTMLUnknownElement.callCallback (react-dom.development.js:540)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:579)
    at invokeGuardedCallback (react-dom.development.js:436)
    at renderRoot (react-dom.development.js:10357)

Using Chrome Browser, with project dependencies

"dependencies": {
    "react": "^16.1.0",
    "react-dom": "^16.1.0",
    "styled-components": "^2.2.3"
  }

@mxstbr @geelen

Issue Analytics

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

github_iconTop GitHub Comments

18reactions
morajabicommented, Nov 13, 2017

@mxstbr What the hell is this, Max: (!!!)

import ThemeProvider from 'styled-components';

@mukurpuri You need to delete that and change the import to this:

import styled, { ThemeProvider } from 'styled-components';
3reactions
mukurpuricommented, Nov 13, 2017

@morajabi Thank you brother for pointing this silly mistake. But there was no point to make a laugh out of this. If it was so silly, I had rather appreciate the explanation but not an emojical fun.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Usage - styled-components
This component provides a theme to all React components underneath itself via the context API. In the render tree all styled-components will have...
Read more >
How to use Themes in styled-components - DEV Community ‍ ‍
First things first, import React from the react dependency, and then we import ThemeProvider from the styled-components file. Theme provider ...
Read more >
Theming and Theme Switching with React and styled ...
Let's set up a project with React and styled-components. ... This component is responsible for providing the theme to all other React ...
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 >
Theme with styled-components and Typescript - Medium
Create a theme. Once we defined our theme interface, we can import DefaultTheme to our theme file and assign all theme values. ·...
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