【发布时间】:2020-01-17 12:32:56
【问题描述】:
此代码有效:
import React from 'react'
import { MyComponent } from './components'
export const NewComponent = props => {
const {
isValid,
} = props
const UseComponent = MyComponent
return (
<>
<UseComponent />
</>
)
}
但是这段代码不工作:
import React from 'react'
import { MyComponent } from './components'
export const NewComponent = props => {
const {
isSet,
} = props
const UseComponent = isSet && MyComponent
return (
<>
<UseComponent />
</>
)
}
即,我正在尝试查看是否正在使用道具 isSet。如果正在使用它,那么我想渲染组件。如果不是,那就不是。
但是当我尝试将其分配给变量时,我收到以下错误消息:
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
有没有办法将我的组件分配给一个变量,以便它在使用道具时呈现,但在不使用时不呈现?
【问题讨论】:
标签: javascript reactjs react-component