styled-components: Uncaught Error: Cannot create styled-component for component: undefined
I’m getting this type of error with a very simple component:
Uncaught Error: Cannot create styled-component for component: undefined
at constructWithOptions (styled-components.es.js:2109)
at styled (styled-components.es.js:2044)
at eval (index.js:43)
at Object.<anonymous> (bundle.js:2569)
at m (bundle.js:1)
at t (bundle.js:1)
at eval (index.js:48)
at Object.<anonymous> (bundle.js:101)
at m (bundle.js:1)
at t (bundle.js:1)
This component produces the error:
import {Container} from '../../components/index.js'
const ProfileContainer = styled(Container)`
width: 100%;
`
class Profile extends Component {
render() {
return (
<ProfileContainer />
)
}
}
export default Profile
Here is the file that I export my components:
import Container from '../components/Container'
import Box from '../components/Box'
import List from '../components/List'
export {
Container,
Box,
List
}
Here is the imported Container component:
import React, {Component} from 'react'
import styled from 'styled-components'
const Container = styled.div`
display: ${props => props.display};
grid-template-columns: ${props => props.gridtemplatecolumns};
overflow-x: ${props => props.overflowx};
overflow-y: ${props => props.overflowy};
`
export default Container
There is no issue if I import the Container component directly but I would rather keep using my export file so I can do things like this:
import {Container, Box, List} from '../../components/index.js'
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 45
- Comments: 29 (4 by maintainers)
Commits related to this issue
- [디버그] styled 버그 수정 components index에서 import한 component를 styled 함수에 인자로 주면 오류 일어남 https://github.com/styled-components/styled-components/issues/1449 — committed to hyorard-b/forte-diet by hyorard-b 3 years ago
- styled-component fails See https://github.com/styled-components/styled-components/issues/1449, but the proposed solution does not work for me. FAIL app/components/admin/SlugInput/SlugInput.test.ts... — committed to CitizenLabDotCo/citizenlab by nTraum 2 years ago
I had the exact same issue and was able to get around it after I saw this comment on another repo. Basically, the trick was this:
I did not have to address any sort of circular import issue, although I should be encountering the exact same problem. I have a single central
indexfile where I exportComponentA(which is styled). I then import that in another file (from theindex), extend the styles as I talked about above to createComponentB, which I use in makingComponentC, which then gets exported in the exact sameindexfile.No clue why this trick is working for me – if somebody knows and could tell me, I’d love to learn, thanks.
@DoctypeRosenthal probably it’s not a problem with the package but with the application codebase itself.
The solution by @VinceSJ does work and it helped us, but it’s an overcome. Together with @agnieszkaac we’ve found out the problem were circular dependencies within our codebase formed by JS/TS
importstatements. Imports are processed synchronously and if there is a cycle, then JS might continue with a certain imported value beingundefinedThe solution that worked perfectly for us is:
madgenpm package which processes given codebase and build depencency graphs. We use it to find cycles.--circularflag (e.g.madge src/index.tsx --warning --circular)husky(will break build/git hook if circular dependency found)baseUrlcompiler option which allows to replace relative import paths (e.g.from ../../../components/x/y/z) with absolute paths based on the directory (e.g.from components/x/y)Often, using index files (
index.jswhich includesexport * from './xyz') introduces circular dependencies. A quick fix in many cases is to replaceimport A from './lib'whereindex.jswas re-exporting the contents with direct file `import A from ‘./lib/a’.Again, @VinceSJ quick fix helped us to carry on, but after we removed the root cause, the quick fix was not needed anymore 😃
There’s some pieces of the puzzle missing here I think;
This signifies most likely that
Containerinconst ProfileContainer = styled(Container)is undefined (try to debug it there; also, why try to debug it with the minified build?)That’s fair but there’s something going wrong in that file then, I’d say if the reexporting file causes the later imported Container to eventually be undefined.
One weird thing I noticed is:
import Container from '../components/Container'vsimport {Container} from '../../components/index.js'is this just accidental?
Also, try to check for circular imports; sometimes they can cause this behaviour.
i.e.
components/A <=> components/index <=> components/Bwould cause either A to beundefinedinBorBto beundefinedin A.This can only be resolved by not going via the
index(reexporting) file in those files that are being reexportedIs this issue resolved I am facing the same issue
I am facing with same error. I have 1 generic index.tsx where I do export of all my components. But some components e.g. ChatRoom is using TextBox so when I had ChatRoom export before TextBox it fails.
// FAILS export { default as ChatRoom } from ‘./ChatRoom’ export { default as TextBox } from ‘./Fields/TextBox’
// WORKS export { default as TextBox } from ‘./Fields/TextBox’ export { default as ChatRoom } from ‘./ChatRoom’
I hope that this helps.
@VinceSJ solution fixed this for me as well… my setup (which I imagine is pretty common) is that I have an index file for common components, which exports them all in this fashion, so I can do things like
import {X, Y, Z} from 'components/common':index.js
TwoColumnis able to directly import and useFlexfine with vanilla React; it’s only when I tried to change this toconst Blabla = styled(Flex)inside ofTwoColumnthat I see the error, so seems like importing should also be supported by SC. This is a nice module pattern so it would be good if this was resolved.Had the same issue when Importing
import { x } from '..'..references a index.ts and that exports the exports of another index.ts inside a folder.When I changed my import to
import { x, y, z } from '../x-folder'it suddenly worked for me. Maybe this might helpTo be clear, since it hasn’t been clearly stated, this is not a bug in styled-components but a project/build level issue, hence not something we can fix 😀
There’s a Webpack plugin which warns you about it: https://www.npmjs.com/package/circular-dependency-plugin
I don’t think you’d have to check all even, but you can start one by one as you see these errors, since that’s how you can confirm the theory
still facing this issue, wonder why they closed it !
Also renaming
export const MyComponenttoexport function MyComponentworked for me as wellI’m using babel-plugin-transform-imports with a configuration like this
That way I can still import them via
import {Box, Text} from '@components'without having to worry about circular dependencies.Thanks @kitten! It appeared to be indeed a project issue: forgot to set up mock for svgr components in jest environment, so styled-components was trying to wrap and style
undefined.I was facing the same issue. I solved it by mocking the dependency that was causing this issue.
@pak11273 actually, as I mentioned in my comment before, you can create an index file. Just don’t use it in other components that are also in the index file.
in my case was circular dependencies as well, I was trying to use a component from the same folder which has an index with a switch that contains both
How could one do this with TypeScript? I also have this issue
I just came across this issue while running tests with
jest26.6.3,jest-styled-components7.0.4, andstyled-components5.2.1 on Node v12. I know we have to upgrade our dependencies, but in case some other unfortunate soul happens across this bug on a similar setup, I resolved the error by updating the export of the original component fromconstto a named function, e.g.,I got the
Cannot create styled-component for component: undefinederror because I was using transform-es2015-modules-commonjs as a fix for an issue I had using jest. Only using the transform-es2015-modules-commonjs plugin during testing, instead of always using it, fixed it.It’s probably not related though.