FlatListDemo的props需要传入arr,和 需要渲染的renderItem,需要排除data属性文章来源:https://uudwc.com/A/0rM1A
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://uudwc.com/A/0rM1A
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} />
)
}