9

我正在使用 TypeScript 构建一个 React Native 应用程序。我正在尝试使用SectionList. 我遵循了文档,这是我的代码:

  renderSectionHeader = ({ section: { title } }: { section: { title: string } }) => (
    <ListItem title={title} />
  );

  render() {
    const { sections } = this.props;
    return (
      <SafeAreaView style={styles.container}>
        <SectionList
          keyExtractor={this.keyExtractor}
          sections={[
            {title: 'Title1', data: ['item1', 'item2']},
            {title: 'Title2', data: ['item3', 'item4']},
            {title: 'Title3', data: ['item5', 'item6']},
          ]}
          renderItem={this.renderItem}
          renderSectionHeader={this.renderSectionHeader}
        />
      </SafeAreaView>
    );
  }

但是该行renderSectionHeader={this.renderSectionHeader}会引发以下 TSLint 错误:

[ts]
Type '({ section: { title } }: { section: { title: string; }; }) => Element' is not assignable to type '(info: { section: SectionListData<any>; }) => ReactElement<any> | null'.
  Types of parameters '__0' and 'info' are incompatible.
    Type '{ section: SectionListData<any>; }' is not assignable to type '{ section: { title: string; }; }'.
      Types of property 'section' are incompatible.
        Type 'SectionListData<any>' is not assignable to type '{ title: string; }'.
          Property 'title' is missing in type 'SectionListData<any>'. [2322]

SectionList破碎的种类吗?还是例子错了?还是我做错了什么?

4

6 回答 6

1
interface Data {
...
}

const MySectionList = SectionList as SectionList<Data>;

<MySectionList
...
/>

为我工作

于 2019-07-28T06:41:07.350 回答
0

这是SectionListData声明,因此您只需删除该title属性,并将其替换为key,然后TSLint Error就会消失。

export interface SectionBase<ItemT> {
    data: ReadonlyArray<ItemT>;

    key?: string;

    renderItem?: SectionListRenderItem<ItemT>;

    ItemSeparatorComponent?: React.ComponentType<any> | null;

    keyExtractor?: (item: ItemT, index: number) => string;
}

export interface SectionListData<ItemT> extends SectionBase<ItemT> {
    [key: string]: any;
}

在此处输入图像描述

于 2019-11-28T08:47:56.440 回答
0

我是 TypeScript 的新手,所以这个问题可能不是最好的,但你可以在这里查看 React Native 类型:React Native Types github

现在,在第 4243 行中,您可以看到:

renderSectionHeader?: (info: { section: SectionListData<ItemT> }) => React.ReactElement<any> | null;

这意味着 renderSectionHeader 属性需要一个带有一个参数的函数,该参数是一个具有 section 字段SectionListData<ItemT>类型的对象。

要摆脱您发布的错误,您可以执行以下操作:

  renderSectionHeader = ({ section: { title } }: { section: { title: string } }): React.ReactElement<any>=> (<ListItem title={title} />)

  render() {
    const { sections } = this.props;
    return (
      <SafeAreaView style={styles.container}>
        <SectionList
          keyExtractor={this.keyExtractor}
          sections={[
            {title: 'Title1', data: ['item1', 'item2']},
            {title: 'Title2', data: ['item3', 'item4']},
            {title: 'Title3', data: ['item5', 'item6']},
          ]}
          renderItem={this.renderItem}
          renderSectionHeader={({section}: {section: SectionListData<string[]>}) => this.renderSectionHeader(section)}
        />
      </SafeAreaView>
    );
  }

希望这是正确的,并会帮助你。

编辑:如果您不想在传递此 renderHeader 方法的道具期间提供类型,则不会出错:

renderSectionHeader = ({ section }: {section: SectionListData<string[]>}): ReactElement<any> | null => (<Text>{section.title}</Text>)
于 2018-12-26T21:28:01.387 回答
0
[
  { title: "Title1", data: ["item1", "item2"] },
  { title: "Title2", data: ["item3", "item4"] },
  { title: "Title3", data: ["item5", "item6"] },
]

首先,您需要定义 Section Type& the date item Type

我将在这里使用一个接口来定义它们。


type Item = string

interface Section {
  title: string;
  data: Item[]
}

now you need to define your `SectionList` `renderItem` function

const renderItem: SectionListRenderItem<Item, Section> = ({ item }) => {
  // make sure you return some sort of a component here.
};

于 2020-12-27T08:42:08.160 回答
-1

以下对我有用

interface Group {
  title: string;
  data: readonly Item[]; // Important to add readonly
}

private scroll: RefObject<SectionList<Item>>;

public renderItem = ({ item }: ListRenderItemInfo<Item>): ReactElement => (
  <ReactItem />
);

public renderSectionHeader = ({
  section,
}: {
  section: SectionListData<Item>;
 }): ReactElement | null => (
  <ReactSectionHeader />
)

const groupedItemToRender = [
  { title: 'title1', data: [ItemObject, ItemObject] },
  { title: 'title2', data: [ItemObject, ItemObject] },
];

<SectionList
  ref={this.scroll}
  sections={groupedItemToRender}
  renderItem={this.renderItem}
  renderSectionHeader={this.renderSectionHeader}
/>
于 2020-06-10T17:49:39.083 回答
-2

如果您知道您正在使用什么类型,并且只是为了避免您可以执行的警告:

sections={sections as any[]}
于 2020-05-22T14:09:40.227 回答