Problem with using imported styled components
See original GitHub issueI 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:
- Created 7 years ago
- Reactions:6
- Comments:19 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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?
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.