Categories
TypeScript Answers

How to extend a component with additional properties with React and TypeScript?

Spread the love

To extend a component with additional properties with React and TypeScript, we can create our own interface.

For instance, we write

export interface AnimalTableProps extends DataTableProps {
  onClickFunction: Function;
}

export class DataTable<T extends DataTableProps> extends React.Component<
  T,
  {}
> {}

export class AnimalTable extends DataTable<AnimalTableProps> {
  render() {
    //...
  }
}

to create the AnimalTableProps interface.

Then we use that as the argument for DataTable assuming it’s a generic type.

And then we can use the properties listed in AnimalTableProps and DataTableProps without compiler errors.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.