Styled Component rendering triggers Element type error

蓝咒 提交于 2020-07-18 21:28:30

问题


I just installed the styled-components library and started playing with it.

However this simple example:

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

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

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

And the render function:

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>
    )
  )
}

triggers this error: Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

The styled component returns an object of $$typeof: Symbol(react.element)

I don't know what goes wrong


回答1:


Which version of React, React-Dom and Styled-Component are you using?

Make sure that react and react-dom are >=16.3.0 with styled-component >=4.0.0

The breaking change brought upong the use of the new Context api in styled-component v4 requires react >=16.3.0

see point #2 on https://www.styled-components.com/docs/faqs#what-do-i-need-to-do-to-migrate-to-v4



来源:https://stackoverflow.com/questions/52973213/styled-component-rendering-triggers-element-type-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!