本文介绍了如何在使用打字稿的本机反应中使用通用组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如 export class FlatListextends React.Component 包含 ItemT 泛型类型.如何在 .tsx 代码中使用它?未参数化看起来像这样:

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}
        />

但我希望我的 renderItem 方法有一个特定的类:

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

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

推荐答案

Typescript 2.9 添加了对 tsx 标签显式指定类型参数的支持.这是PR.所以应用语法,它应该是:

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}
    />

这篇关于如何在使用打字稿的本机反应中使用通用组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-22 06:49