javascript - 无法读取未定义的属性 'styledComponentId'

标签 javascript css styled-components

我使用 styled-component 但找不到找到匹配元素的方法。我得到了

的错误

TypeError:无法读取未定义的属性“styledComponentId”

这是我的 index.js const Container = styled.h1` 字体大小:14px;

import React from 'react'
import styled from 'styled-components'
const Container = styled.h1`
  font-size: 14px;
`

export default () => (<div>
    <Container>Hello</Container>
</div>)
export { Container }

这是我的测试(index.spec.js)

import React from 'react'
import { shallow } from 'enzyme'
import IndexPage from './index'
import { Container } from './index'
import { enzymeFind } from 'styled-components/test-utils'

describe('Pages', () => {
  describe('Index page', () => {
    it('should have a container', function() {
      const wrapper = shallow(<IndexPage />)
      expect(enzymeFind(wrapper, Container).exists()).toBe(true)
    })
  })
})

最佳答案

在您的 index.spec.js 中,您有:

import IndexPage from './index'
import { Container } from './index'

然而,您发布的代码只有 index.js 中的默认导出 - 您可能正在混合要从中导入的文件。

关于javascript - 无法读取未定义的属性 'styledComponentId',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54209688/

相关文章:

javascript - 在浏览器选项卡之间切换时动画不会暂停

javascript - D3 折叠树 : Scrollable container for tree

python - 找不到 Django Bootstrap 文件

css - styled-components 的生产版本中我的 css 类在哪里

javascript - 样式化组件未包装另一个样式化组件

javascript - 如何在背景设置为图像的完美正方形中创建元素网格(div/spans)

javascript - 如何从node.js中的模块导出函数

css - 区分:focus via tab-key and :focus via click in CSS

css - 我应该使用媒体查询进行响应式设计吗?

javascript - 在 AntD 中更改 form.item 标签字体大小