TpoM6oH
TpoM6oH

Reputation: 8585

How to use generic component in react native with typescript

For example export class FlatList<ItemT> extends React.Component<FlatListProps<ItemT>> has ItemT generic type in it. How do I use it in .tsx code? Not parametrized looks like this:

<FlatList
          data={this.state.data}
          keyExtractor={this.keyExtractor}
          renderItem={this.renderItem}
        />

But I would like my renderItem method to have a specific class:

renderItem = (item: ListRenderItemInfo<DataItem>) => {

Upvotes: 4

Views: 5267

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 250266

Typescript 2.9 has added support to explicitly specify the type parameter to a tsx tag. This is the PR for this. So applying the syntax, it should be:

<FlatList<DataItem>
      data={this.state.data}
      keyExtractor={this.keyExtractor}
      renderItem={this.renderItem}
    />

Upvotes: 4

Related Questions