简体   繁体   English

在三元操作中使用 react JSX 和 HTML 并且无法正确显示它

[英]Using react JSX and HTML in a ternary operation and cannot get it to display it correctly

the variable twitter_username resolves to either a username or null.变量 twitter_username 解析为用户名或空值。

 {{twitter_username}?<p>@{twitter_username}</p>:null}

I want this to display @username if there is a username or nothing at all if the value is null.如果有用户名,我希望它显示@username,如果值为空,则根本没有。 If there is a username it displays fine but if the return is null I get an @ instead of nothing.如果有用户名,它显示正常,但如果返回为空,我会得到一个 @ 而不是什么。

 {{twitter_username}?<p>{twitter_username}</p>:null}

this code returns the username if there is a username and is blank when there is none.如果有用户名,此代码返回用户名,没有时返回空白。

a nice and easy way to do it { twitter_username && (<p>@{twitter_username}</p>) }一个很好且简单的方法{ twitter_username && (<p>@{twitter_username}</p>) }

Here the left part of the ternary operator will continue to the right only if the first part (twitter_username) if defined.此处三元运算符的左侧部分将继续向右,仅当第一部分 (twitter_username) 已定义时。 The right part is a return statement who returns jsx右边是返回jsx的return语句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM