React Native Flatlist 错误没有重载与此调用匹配

Joa*_*ios 5 typescript reactjs react-native react-navigation react-native-flatlist

我正在使用 Typescript 制作 React Native 应用程序,但 Flatlist renderItem 出现错误。我是 Typescript 和 React Native 的新手。错误说:

没有重载与此调用匹配。重载第 1 个(共 2 个)“(props: FlatListProps | Readonly<FlatListProps>): FlatList<...>”出现以下错误。类型“({ item }: { item: arrayPlasticsData; }) => void”不可分配给类型“ListRenderItem”。类型“void”不可分配给类型“ReactElement<any, string |” JSXElement构造函数> | 无效的'。重载 2 个(共 2 个)“(props: FlatListProps, context: any): FlatList”,出现以下错误。类型“({ item }: { item: arrayPlasticsData; }) => void”不可分配给类型“ListRenderItem”。

我正在尝试渲染一个包含项目列表的 Flatlist,但它基本上不允许我在那里渲染任何内容。我也尝试在 renderItem 上放置类似 Something 的内容,但它也不起作用。PlasticTypesComponent.ts 带来一个包含数据的数组。这是代码:

import { FlatList, StyleSheet, Text, View } from "react-native";
import PlasticTypesComponent, { arrayPlasticsData } from "../../components/data/PlasticTypes";

import PlasticItemComponent from "../../components/plasticItem/plasticItem";
import React from "react";

interface plasticsScreenComponentProps {
    route: any
    navigation: any
    renderItem: any
}
 
const plasticsScreenComponent: React.FC<plasticsScreenComponentProps> = ({ navigation, route }) => {
    const plasticTypes = PlasticTypesComponent.filter(plastic => plastic.category === route.params.categoryID);
    console.log('plasticTypes', plasticTypes)

    const handleSelected = (item: { id: string; title: string; }) => {
        navigation.navigate('Plastic description', {
            productID: item.id,
            name: item.title,
        });
    };
    const renderItemPlastic = ({item}: {item: arrayPlasticsData}) => {
        <PlasticItemComponent item={item} onSelected={handleSelected} />
//// here is where I tried to replace <PlasticItemCompoenent/> with <Text>Something</Text> but didn't work either
    };
    return (
        <>
            <View style={styles.container}>
                <Text style={styles.text}>plastics</Text>
                <FlatList 
                data={plasticTypes}
                keyExtractor={item => item.id}
                renderItem={renderItemPlastic} />
            </View>            
        </>
    );
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        alignContent: 'center',
        justifyContent: 'center',
    },
    text: {
        fontSize: 23,
        textAlign: 'center',
    },
});
export default plasticsScreenComponent;
Run Code Online (Sandbox Code Playgroud)

也许是我做错了 Typescript 的某些事情。我找了几天也没找到解决办法。如果有人可以帮助我,那就太好了。提前致谢。

nit*_*npp 7

你必须JSX从你的renderItemPlastic函数中返回你的。只需将您的renderItemPlastic功能更改为

const renderItemPlastic = ({item}: {item: arrayPlasticsData}) => {
    return <PlasticItemComponent item={item} onSelected={handleSelected} />
};
Run Code Online (Sandbox Code Playgroud)

或者简单地

const renderItemPlastic = ({item}: {item: arrayPlasticsData}) => 
    <PlasticItemComponent item={item} onSelected={handleSelected} />;
Run Code Online (Sandbox Code Playgroud)