我正在使用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
的类型是否损坏?还是这个例子错了?还是我做错了什么?
答案 0 :(得分:0)
我是TypeScript的新手,所以这个问题可能不是最好的,但是您可以在此处检查React Native类型:React Native Types github
现在在4243行中,您可以看到以下内容:
renderSectionHeader?: (info: { section: SectionListData<ItemT> }) => React.ReactElement<any> | null;
这意味着renderSectionHeader属性需要一个带有一个参数的函数,该参数是具有SectionListData<ItemT>
类型的section字段的对象。
要摆脱发布的错误,您可以执行以下操作:
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>)
答案 1 :(得分:0)
interface Data {
...
}
const MySectionList = SectionList as SectionList<Data>;
<MySectionList
...
/>
为我工作
答案 2 :(得分: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;
}
答案 3 :(得分:0)
如果您知道使用的是哪种类型,只是为了避免出现警告,您可以这样做:
sections={sections as any[]}
答案 4 :(得分:0)
以下对我有用的
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}
/>
答案 5 :(得分:0)
[
{ title: "Title1", data: ["item1", "item2"] },
{ title: "Title2", data: ["item3", "item4"] },
{ title: "Title3", data: ["item5", "item6"] },
]
首先您需要定义部分 Type
和日期项 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.
};