[英]React component. Render specific element
Currently I've a component User , which renders 2 element -> username and avatar .目前我有一个组件User ,它呈现 2 element -> username和avatar 。 I'm getting the username and avatar perfectly, but I want to view only the username only
我完美地获取了用户名和头像,但我只想查看用户名
Is there any way to fetch only the username element?有没有办法只获取用户名元素? Not with a profile picture.
没有个人资料照片。
//User component
const User = ({ username, profilePic }) => {
return (
<React.Fragment>
<Avatar name='user' src={profilePic.profile.image} alt="user_image" ml="5"/>
<Heading size={'sm'} ml="5">{username.username}</Heading>
</React.Fragment>
);
};
// Content Page
{group.members.map(member => {
return <React.Fragment key={member.id}>
<User username={member.user} profilePic={member.user}/>
</React.Fragment>
})}
You could add an extra prop renderAvatar
and only display the avatar if the boolean is true
with conditional rendering.您可以添加一个额外的道具
renderAvatar
并且仅在 boolean 条件渲染为true
时才显示头像。
const User = ({ username, profilePic, renderAvatar }) => {
return (
<React.Fragment>
{renderAvatar && <Avatar name='user' src={profilePic.profile.image} alt="user_image" ml="5"/>}
<Heading size={'sm'} ml="5">{username.username}</Heading>
</React.Fragment>
);
};
You could use it like this.你可以像这样使用它。
<User username={member.user} profilePic={member.user} renderAvatar={false} />
<User username={member.user} profilePic={member.user} renderAvatar={true} />
Or just create a component that only renders the Heading
.或者只是创建一个只呈现
Heading
的组件。
const UserWithoutAvatar = ({ username }) => {
return <Heading size={'sm'} ml="5">{username.username}</Heading>
};
One option would be to conditionally render based on whether or not profilePic
is provided at all.一种选择是根据是否提供
profilePic
有条件地呈现。 For example:例如:
return (
<React.Fragment>
{ profilePic ?
<Avatar name='user' src={profilePic.profile.image} alt="user_image" ml="5"/>
: null
}
<Heading size={'sm'} ml="5">{username.username}</Heading>
</React.Fragment>
);
Then if you just don't provide profilePic
it will be undefined
:那么如果你不提供
profilePic
它将是undefined
:
<User username={member.user} />
As an aside, the code seems to be generating confusion around naming.顺便说一句,代码似乎在命名方面产生了混乱。 For example:
例如:
{username.username}
A property called "username" implies that it is a string representing the user's name.名为“username”的属性意味着它是一个表示用户名的字符串。 But in this case
username
is an object containing a property called username
?但在这种情况下,
username
是一个 object,其中包含一个名为username
的属性? Does that property contain a string?该属性是否包含字符串? Or another object?
或者另一个 object?
Or here:或者在这里:
<User username={member.user} profilePic={member.user}/>
What is member.user
?什么是
member.user
? Is it a username?是用户名吗? Is it a profile pic?
这是个人资料照片吗? Somehow it's both?
不知何故两者兼而有之?
Clarity is important.清晰度很重要。 If what you're actually passing to the component is a user object then call it that:
如果您实际传递给组件的是用户 object ,则称它为:
<User user={member.user} />
Alternatively, if the component is expecting a literal value for username
and a literal value for profilePic
then pass it those values:或者,如果组件需要
username
的字面值和profilePic
的字面值,则将这些值传递给它:
<User username={member.user.username} profilePic={member.user.profile.image} />
Don't confuse your semantics.不要混淆你的语义。 Confusion leads to bugs.
混乱导致错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.