相关疑难解决方法(0)

React/TypeScript:扩展具有其他属性的组件

我正在尝试使用react来重新创建我的current组件(用纯打字稿编写),但我找不到一种方法来为扩展另一个的组件提供额外的道具.

export interface DataTableProps {
    columns: any[];
    data: any[];
}

export class DataTable extends React.Component<DataTableProps, {}> {
   render() {
       // -- I can use this.props.columns and this.props.data --
   }
}

export class AnimalTable extends DataTable {
    render() {
       // -- I would need to use a this.props.onClickFunction -- 
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是我需要给AnimalTable一些与DataTable无关的道具.我怎样才能做到这一点 ?

typescript reactjs

18
推荐指数
4
解决办法
2万
查看次数

如何在扩展 React 组件的抽象类中使用泛型?

我正在创建一个扩展 React 组件的抽象类,我希望能够设置一些默认的道具,但也让扩展抽象类的组件提供自己的道具。

interface Props {                                                                       
  someProps: boolean                                                                    
}                                                                                       

abstract class AbstractPureForm<P, S> extends React.Component<Props & P, S> {
  ...
}
Run Code Online (Sandbox Code Playgroud)

如何使用:

class Other extends AbstractPureForm<{ newProps: string}, {}> { 
  ...
}
Run Code Online (Sandbox Code Playgroud)

现在这个设置给了我错误:

does not exist on type '(Props & P)["..."]'
Run Code Online (Sandbox Code Playgroud)

typescript reactjs

2
推荐指数
1
解决办法
2457
查看次数

标签 统计

reactjs ×2

typescript ×2