[英]React-native & typescript: Conditionally assign component to variable
Can anyone assist with error below: JSX element type 'XXX' does not have any construct or call signatures任何人都可以协助解决以下错误:JSX 元素类型“XXX”没有任何构造或调用签名
I want to render component conditionally我想有条件地渲染组件
const RenderingComponent = someCondition ? TouchableNativeFeedback : TouchableOpacity
return(
<View>
<RenderingComponent onPress={()=>{}} />
</View>
)
Try to explicitly specify the type:尝试明确指定类型:
const RenderingComponent: React.ElementType = someCondition ? TouchableNativeFeedback : TouchableOpacity
Also you can create a wrapper for your condition, eg您也可以为您的条件创建一个包装器,例如
interface Props {
someCondition: boolean;
// Any other props you want to pass
}
const RenderingComponent = (props: PropsWithChildren<Props>) => {
const {someCondition, children} = props;
if (someCondition) {
return < TouchableNativeFeedback {...props}> {children} </TouchableNativeFeedback >
} else {
return <TouchableOpacity {...props}> {children} </TouchableOpacity>
}
}
And then use RenderingComponent
然后使用
RenderingComponent
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.