【发布时间】:2021-01-21 08:49:13
【问题描述】:
我正在学习 react/jsx,但遇到了一个难题。
我想通过尝试在地图中创建地图来“简化”,我以为我知道自己在做什么,但显然它不喜欢它。
我有以下对象:
defaultState = {
userName: "",
firstName: "",
lastName: "",
email: "",
bio: "",
country: "",
notifications: [
{
label: "Newsletter",
value: "newsletter",
checked: false,
},
{
label: "Updates",
value: "updates",
checked: false,
},
{
label: "Reports",
value: "reports",
checked: false,
},
]
};
本质上,我想在 defaultState 数据中制作通知地图。这样我就有了一张包含以下信息的卡片
- firstName={user.firstName}
- lastName={user.lastName}
- 用户名={user.userName}
- email={user.email}
- bio={user.bio}
- {user.notifications[0].value} ={user.notifications[0].checked}
- {user.notifications[1].value} ={user.notifications[1].checked}
- {user.notifications[2].value} ={user.notifications[2].checked}
所以它看起来像这样
- 用户名:ausername123
- 名字:A.
- 姓:W.
- 邮箱:blahblah@gmail.com
- 简历:有说明
- 报纸通知:真
- 更新通知:错误
- 报告通知:真
到目前为止,我已经写了以下的地图
{this.state.users.map((user) => {
return(
<User
key={user.userName}
firstName={user.firstName}
lastName={user.lastName}
userName={user.userName}
email={user.email}
bio={user.bio}
country={user.country}
{user.notifications.map((n) => n.value = n.checked)}
/>
)
})}
然而行 {user.notifications.map((n) => n.value = n.checked)} 出现以下错误
SyntaxError: Unexpected token, expected "..." (31:33)
31 | {user.notifications.map((n) => n.value = n.checked)} | ^ 32 | /> 33 | ) 34 | })}
由于一个请求,这是我试图在 User.js 上运行的。本质上我想创建三个附加的道具标记
- 时事通讯
- 更新
- 报告 它们恰好是通知[].label,我想根据选中的复选框给它们一个真或假的值。
const User = ({userName, firstName, lastName, email, bio, country, newsletter, updates, reports}) => (
<Component>
<Header>User Details of {firstName} {lastName}</Header>
<Field>
<Text>
<strong>Username:</strong>
</Text>
<Item>{userName}</Item>
</Field>
<Field>
<Text>
<strong>Email:</strong>
</Text>
<Item>{email}</Item>
</Field>
<Field>
<Text>
<strong>Bio:</strong>
</Text>
<Item>{bio}</Item>
</Field>
<Field>
<Text>
<strong>Country:</strong>
</Text>
<Item>{country}</Item>
</Field>
<Field>
<Text>
<strong>Newsletter:</strong>
</Text>
<Item>{newsletter}</Item>
</Field>
<Field>
<Text>
<strong>Updates:</strong>
</Text>
<Item>{updates}</Item>
</Field>
<Field>
<Text>
<strong>Reports:</strong>
</Text>
<Item>{reports}</Item>
</Field>
</Component>
);
【问题讨论】:
-
您必须将通知分配给您将在
User组件中处理的道具;您应该将{user.notifications.map((n) => n.value = n.checked)}更改为notifications={user.notifications.map((n) => n.value = n.checked)} -
@secan 把它写成答案
-
@slebetman,当然,没问题;我不认为它具有正确答案的“尊严”,因为它不是解决某些晦涩问题的方法,并且可以在官方文档中找到或推断出来。
标签: javascript reactjs jsx