javascript - How do I conditionally include a tag in the middle of some HTML in React - Stack Overflow

I have an Avatar React ponent and there's an option to make it link to a profile or not. You may n

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
Add a ment  | 

2 Answers 2

Reset to default 4

Use:

<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>;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745301431a4621457.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信