Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>) => {
like image 248
TpoM6oH Avatar asked Sep 18 '25 13:09

TpoM6oH


1 Answers

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}
    />
like image 170
Titian Cernicova-Dragomir Avatar answered Sep 21 '25 12:09

Titian Cernicova-Dragomir