样式化的组件渲染触发元素类型错误

时间:2018-10-24 15:50:16

标签: reactjs styled-components

我刚刚安装了styled-components库并开始使用它。

但是这个简单的例子:

import React from 'react'
import styled from 'styled-components'

const Div = styled.div`background: red;`

const AlertCard = () => (
  <Div>Hello World</Div>
)
export default AlertCard

渲染功能:

render() {
  const { fetchingAlerts, alerts } = this.props

  return (
    fetchingAlerts ? 
      <Loader /> :
      <div>
        <AlertFormContainer />
        <div>
          {alerts.length === 0 ?
            <EmptyAlerts /> :
            <div>
              {alerts.map(alert => (
                <AlertCard text="My text " />
              ))}
            </div>
          }
        </div>
      </div>
    )
  )
}

触发此错误:Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

样式化的组件返回$$typeof: Symbol(react.element)对象

我不知道怎么了

1 个答案:

答案 0 :(得分:3)

您使用哪个版本的React,React-Dom和Styled-Component?

确保带有样式组件> = 4.0.0的react react-dom为> = 16.3.0

在样式化组件v4中使用新的Context API带来的重大变化要求react> = 16.3.0

参见https://www.styled-components.com/docs/faqs#what-do-i-need-to-do-to-migrate-to-v4上的#2点

相关问题