问题
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