我正在用前端的一些虚拟数据创建一个映射出来的组件。我有一些从js文件中提取的快速数据
export const userInputs = [
{
id: 1,
label: "First Name",
type: "text",
placeholder: "Remy"
},
{
id: 2,
label: "Surname",
type: "text",
placeholder: "Sharp",
},
{
id: 3,
label: "Email",
type: "email",
placeholder: "remysharp@gmail.com",
}
]
然后我把它作为“投入”的支柱,就像这样
<UserProfile inputs={userInputs}/>
我把它作为我的组件的一个道具,但是当我试图将其映射为:
<UpdateUserDetails>
{inputs.map((input) => (
<FormInput key={input.id}>
<UserInput type={input.type} placeholder={input.placeholder}>
</UserInput>
</FormInput>
))}
</UpdateUserDetails>
我得到以下错误×TypeError:无法读取未定义的属性(读取'map')
我遗漏了什么?
发布于 2022-07-22 20:28:23
我现在主要使用React本机(博览:https://docs.expo.dev/),所以我的代码看起来有点不同,但似乎只是缺少索引。请随意使用以下内容
import { StyleSheet, Text, View, TouchableOpacity } from 'react-native'
export const inputs = [
{
id: 1,
label: 'First Name',
type: 'text',
placeholder: 'Remy',
},
{
id: 2,
label: 'Surname',
type: 'text',
placeholder: 'Sharp',
},
{
id: 3,
label: 'Email',
type: 'email',
placeholder: 'remysharp@gmail.com',
},
]
export default function App() {
return (
<View>
{inputs.map((input, index) => (
<TouchableOpacity key={input.id}>
<Text>{inputs[index]?.label}</Text>
</TouchableOpacity>
))}
</View>
)
}
为了简单起见,我只是将userInputs
作为inputs
放在同一个文件中。
我希望这能帮到你。好好休息一下吧!
https://stackoverflow.com/questions/73087822
复制相似问题