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.

Problem with using imported styled components

See original GitHub issue

I have a global file with styled components that I use accross my whole website. When I import these global components to use them directly in React, there is no problem, but if I want to create a new styled components out of them, I get this error:

Uncaught TypeError: Cannot read property 'displayName' of undefined
    at createStyledComponent (eval at <anonymous> (http://localhost:3000/static/js/bundle.js:7296:2), <anonymous>:148:82)
    at eval (eval at <anonymous> (http://localhost:3000/static/js/bundle.js:7350:2), <anonymous>:20:14)
    at eval (eval at <anonymous> (http://localhost:3000/static/js/bundle.js:7575:2), <anonymous>:32:62)
    at Object.<anonymous> (http://localhost:3000/static/js/bundle.js:7575:2)
    at __webpack_require__ (http://localhost:3000/static/js/bundle.js:554:30)
    at fn (http://localhost:3000/static/js/bundle.js:85:20)
    at eval (eval at <anonymous> (http://localhost:3000/static/js/bundle.js:7557:2), <anonymous>:16:20)
    at Object.<anonymous> (http://localhost:3000/static/js/bundle.js:7557:2)
    at __webpack_require__ (http://localhost:3000/static/js/bundle.js:554:30)
    at fn (http://localhost:3000/static/js/bundle.js:85:20)

Here is an extract of the code generating this error:

import React from 'react';
import styled from 'styled-components';
import {Div} from '../../global/layout';

const RoundedBar = styled(Div)`
  transition: all 500ms;
  border-radius: 15px;
  height: 15px;
  width: ${props => props.width || "100%"}
`;

If I put this RoundedBar SC inside the global file, there is no error. If I use styled.div instead, there is also no error.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:6
  • Comments:19 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
jimmyheaddoncommented, Feb 3, 2017

TypeScript moans that the JSX element doesn’t support those props, but I can’t pass the props interface the component is using either as that fails. Short of having the styled-component in the same .tsx file I’m not sure how you get the props in?

2reactions
srosset81commented, Feb 1, 2017

Any help or pointing out my glaring errors would be much appreciated!

I keep on getting this error pretty often and it is always due to some sort of cyclic import (ie. A imports B which imports C, which is itself importing A). Cyclic import is generally a sign of bad code architecture, I know. Nonetheless, it seems styled-components doesn’t like them at all.

Read more comments on GitHub >

github_iconTop Results From Across the Web

I am using styled from styled-components but it is not working ...
You have imported styled-components incorrectly. use import styled from 'styled-components';.
Read more >
FAQs - styled-components
In general, always use the css helper when composing styling partials to be interpolated into a styled component.. import styled, { keyframes } ......
Read more >
The Pros and Cons of Using Styled Components in React
For applications that use styled components, the browser downloads the CSS and parses it using JavaScript before injecting them into the page.
Read more >
How To Use React Styled Components Efficiently - Copycat
// Later in your app, you can go ahead to import and add the component along with every other top-level component you have...
Read more >
styled() - MUI System
Utility for creating styled components. ... if you are using @mui/material , you can import it from @mui/material/styles . ... What problems does...
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