如何使用 Facebook React 的 JSX 拥有条件元素并保持 DRY? [英] How to have conditional elements and keep DRY with Facebook React's JSX?
本文介绍了如何使用 Facebook React 的 JSX 拥有条件元素并保持 DRY?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
How do I optionally include an element in JSX? Here is an example using a banner that should be in the component if it has been passed in. What I want to avoid is having to duplicate HTML tags in the if statement.
render: function () {
var banner;
if (this.state.banner) {
banner = <div id="banner">{this.state.banner}</div>;
} else {
banner = ?????
}
return (
<div id="page">
{banner}
<div id="other-content">
blah blah blah...
</div>
</div>
);
}
解决方案
Just leave banner as being undefined and it does not get included.
这篇关于如何使用 Facebook React 的 JSX 拥有条件元素并保持 DRY?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文