Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

关于React组件类型的问题

const Demo = (props)=>{
    return [
        <Foo/>,
        <Bar/>
    ]
}

这样一个组件怎么用typescript重写呢?
尝试写成

interface Props{
}
const Demo:React.FunctionComponent<Props> = (props)=>{}

或者

const Demo:React.ReactElement

或者

const Demo:React.Component

都会报类型错误.
写成下面这种是可以的

const Demo:(props:Props)=>React.ReactNode = (props)=>{
    return [
        <Foo/>
        <Bar/>
    ]
}

但是这种的在使用Demo组件时又报错

Its return type 'ReactNode' is not a valid JSX element.

我的问题是:如果一个函数式组件返回一个没有rootElement的jsx,用ts应该怎么写?

又自问自答了


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...