admin管理员组文章数量:1419244
I have an Avatar
React ponent and there's an option to make it link to a profile or not. You may not want it to link to a profile if, say, your on the users profile and instead you want to do a custom clickHandler
. Is there a nicer way other than just doing an if/else with basically identical HTML in each if and else other than a link? Below is some pseudo rendering code just to show an example of what I mean:
<div className={"Avatar Avatar--" + this.props.size} onClick={this.props.clickHandler}>
{if (this.props.link) {
<Link to="profile" params={{userId:this.props.user.id}}>
}
}
<img className="__avatarimage" src={this.props.user.avatar} />
{if (this.props.link) {
</Link>
}
}
</div>
I have an Avatar
React ponent and there's an option to make it link to a profile or not. You may not want it to link to a profile if, say, your on the users profile and instead you want to do a custom clickHandler
. Is there a nicer way other than just doing an if/else with basically identical HTML in each if and else other than a link? Below is some pseudo rendering code just to show an example of what I mean:
<div className={"Avatar Avatar--" + this.props.size} onClick={this.props.clickHandler}>
{if (this.props.link) {
<Link to="profile" params={{userId:this.props.user.id}}>
}
}
<img className="__avatarimage" src={this.props.user.avatar} />
{if (this.props.link) {
</Link>
}
}
</div>
Share
Improve this question
edited Jan 31, 2016 at 5:12
Dmitry Shvedov
3,3164 gold badges40 silver badges56 bronze badges
asked Nov 20, 2014 at 5:49
Oscar GodsonOscar Godson
32.8k42 gold badges125 silver badges206 bronze badges
2 Answers
Reset to default 4Use:
<div className={"Avatar Avatar--" + this.props.size} onClick={this.props.clickHandler}>
{ this.props.link ?
<Link to="profile" params={{userId:this.props.user.id}}>
<img className="__avatarimage" src={this.props.user.avatar} />
</Link>
: <img className="__avatarimage" src={this.props.user.avatar} /> }
You can try to eliminate double definition of img by defining it earlier:
var img = <img className="__avatarimage" src={this.props.user.avatar} />;
and embed it using:
{img}
I'd create a function that either returns the image or the image wrapped in the link and then add it to the div.
var createAvatar = function() {
if (this.props.link) {
return <Link to="profile" params={{userId:this.props.user.id}}>
<img className="__avatarimage" src={this.props.user.avatar} />
</Link>;
} else {
return <img className="__avatarimage" src={this.props.user.avatar} />;
}
};
var avatar = createAvatar();
return <div className={"Avatar Avatar--" + this.props.size} onClick={this.props.clickHandler}>
{avatar}
</div>;
本文标签: javascriptHow do I conditionally include a tag in the middle of some HTML in ReactStack Overflow
版权声明:本文标题:javascript - How do I conditionally include a tag in the middle of some HTML in React - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745301430a2652393.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论