样式化组件渲染触发元素类型错误
Styled Component rendering triggers Element type error
我刚刚安装了 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.
样式组件 returns $$typeof: Symbol(react.element)
的对象
不知道怎么回事
您使用的是哪个版本的 React、React-Dom 和 Styled-Component?
确保 react 和 react-dom >=16.3.0 with styled-component >=4.0.0
在 styled-component v4 中使用新上下文 api 带来的重大变化需要 react >=16.3.0
参见 https://www.styled-components.com/docs/faqs#what-do-i-need-to-do-to-migrate-to-v4
上的第 2 点
我刚刚安装了 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.
样式组件 returns $$typeof: Symbol(react.element)
不知道怎么回事
您使用的是哪个版本的 React、React-Dom 和 Styled-Component?
确保 react 和 react-dom >=16.3.0 with styled-component >=4.0.0
在 styled-component v4 中使用新上下文 api 带来的重大变化需要 react >=16.3.0
参见 https://www.styled-components.com/docs/faqs#what-do-i-need-to-do-to-migrate-to-v4
上的第 2 点