FlatListDemo的props需要传入arr,和 需要渲染的renderItem,需要排除data属性文章来源:https://www.toymoban.com/news/detail-599797.html
import React from 'react';
import { FlatList, Text, View, FlatListProps } from 'react-native';
interface FunctionViewProps<T> extends Omit<FlatListProps<T>, 'renderItem' | 'data'> {
arr: T[];
renderItem: (item: T) => JSX.Element;
}
interface IArrItem {
id: number;
name: string;
}
const FlatListDemo: React.FC<FunctionViewProps<IArrItem>> = ({ arr, renderItem, ...rest }) => {
return (
<FlatList
{...rest}
data={arr}
renderItem={({ item }) => renderItem(item)}
keyExtractor={({ id }) => id.toString()}
/>
);
};
export default FlatListDemo;
app.tsx中使用文章来源地址https://www.toymoban.com/news/detail-599797.html
interface Item {
id: number;
name: string;
}
const arr: Item[] = [
{ id: 1, name: 'JavaScript' },
{ id: 2, name: 'Html' },
{ id: 3, name: 'Css' },
{ id: 4, name: 'Node' },
{ id: 5, name: 'java' },
]
const App = (): JSX.Element => {
const renderListItem = (item: Item) => {
return (
<View style={{ width: '100%', height: 50, backgroundColor: '#cccccc', marginBottom: 10 }}>
<Text style={{ textAlign: 'center', lineHeight: 50 }}>{item.name}</Text>
</View>
);
}
return (
<FlatListDemo renderItem={renderListItem} arr={arr} />
)
}
到了这里,关于【Ts之Omit 排除属性】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!