如何利用反应选择道具类型

问题描述 投票:0回答:1

我正在尝试编写一个包装AsyncSelect的组件,但是在这种情况下,他们的道具类型有一个泛型,我不知道如何实现它。

这是我的代码:

export class PipTagSelect extends React.Component<AsyncProps> {
    constructor(props:AsyncProps ) {
        super(props);
    }

    render() {
        return (
            <AsyncSelect
                isMulti
                cacheOptions
                {...this.props}
            />
        );
     }
}

编译器给出错误AsyncProps<OptionType>需要一个类型参数。在查看类型定义时,这是有意义的。

但是,在包装组件时,我从来没有必要为props提供类型参数。我不确定我应该做什么。

reactjs typescript react-select
1个回答
2
投票

使你的班级成为通用的

 export class<T> PipTagSelect extends React.Component<AsyncProps<T>> {
    constructor(props:AsyncProps<T>) {
        super(props);
    }

    render() {
        return (
            <AsyncSelect
                isMulti
                cacheOptions
                {...this.props}
            />
        );
     }
 }

检查出on github

© www.soinside.com 2019 - 2024. All rights reserved.