我试图运行一个带有类型记录的Flatlist,但是它似乎只允许一个表达式函数,它的道具是非结构化的。我似乎想不出该怎样用必要的方式(建议)来解释。有人知道怎么解决这个问题吗?
不变平面函数
const renderSuggestions = () =>
<FlatList
data={data}
renderItem={renderItem}
onPress={(item:Suggestion) => handleSelect(item)}
keyExtractor={item => item.place_id}
/>尝试1(错误:未找到renderItem重载)
const renderItem = (item:Suggestion)=> (
<View>
<TouchableOpacity >
<Text >{item.structured_formatting.main_text}</Text>
</TouchableOpacity>
</View>
)尝试2(类型记录类型错误(隐式任何类型))
const renderItem = ({item})=> (
<View>
<TouchableOpacity >
<Text >{item.structured_formatting.main_text}</Text>
</TouchableOpacity>
</View>
)每个答案尝试:项/索引未找到错误
const renderItem = (item:Suggestion, index:number)=> (
<View>
<TouchableOpacity >
<Text >{item.structured_formatting.main_text}</Text>
</TouchableOpacity>
</View>
)
const renderSuggestions = () =>
<FlatList
data={data}
renderItem={({ item:Suggestion, index:number }) => renderItem(item, index)}
onPress={(item:Suggestion) => handleSelect(item)}
keyExtractor={item => item.place_id}
/>发布于 2022-03-27 20:48:54
对于FlatList,传递的data支柱的类型决定了非结构化item的类型。下面是一个最基本的工作示例。
import React, { useState } from "react"
import { FlatList } from "react-native"
type DataType = {
id: number,
someThing: string
}
const FlatListWithType = () => {
// we specify the type here
const [data, setData] = useState<DataType[]>()
return (
<FlatList
data={data}
renderItem={({ item, index }) => (
)}
/>
)
}这种类型的建议现在应该行得通。

https://stackoverflow.com/questions/71640119
复制相似问题