javascript - 如果在 React 15 中嵌套 2 的最佳方法是什么?
问题描述
我想知道是否可以在 reactjs jsx 中进行嵌套 2 ifs?我尝试了各种不同的方法,但我无法让它工作。我只尝试了一个有效的元素。一旦我放置和元素,我的代码就会中断。
<div>
{
(() => {
if (VerticalRole === "Software Developer")
<div>
<label>GITHUB ACCOUNT</label>
<div className="value">
<input
type="text"
name="github_url"
className="lowercase"
defaultValue={this.props.talent.github_url}
onChange={this.onChangeInput.bind(this)}
/>
</div>
</div>
if (MarketVetical === "Creative/Marketing")
<div>
<label>Portfolio Website</label>
<div className="value">
<input
type="text"
name="github_url"
className="lowercase"
defaultValue=
{this.props.talent.personal_website}
onChange={this.onChangeInput.bind(this)}
/>
</div>
</div>
})()
}
</div>
解决方案
您可以尝试使用三元运算符:
<div>
{
VerticalRole === "Software Developer" ?
<div>
<label>GITHUB ACCOUNT</label>
<div className="value">
<input
type="text"
name="github_url"
className="lowercase"
defaultValue={this.props.talent.github_url}
onChange={this.onChangeInput.bind(this)}
/>
</div>
</div>
: MarketVetical === "Creative/Marketing" ?
<div>
<label>Portfolio Website</label>
<div className="value">
<input
type="text"
name="github_url"
className="lowercase"
defaultValue=
{this.props.talent.personal_website}
onChange={this.onChangeInput.bind(this)}
/>
</div>
</div>
: null
}
</div>
推荐阅读
- wordpress - WordPress MultiSite 不加载文件或不允许我登录
- c# - 在两个具有相同值但不在同一个位置的列表中查找差异
- r - 计算不同列数的分组平均值
- python - Python Futures Concurrent 在 Flask 中不起作用
- javascript - 无法在 javascipt 函数中获取输入标记的值?
- javascript - 关闭谷歌应用脚本中的modalDialogue框后运行代码
- python - 为什么我的蟒蛇游戏项目中有这两个错误?
- azure - 从 Azure ADF IR 链接服务连接到 AWS RedShift(启用 SSL)
- javascript - 如何在 JavaScript 中获取已检查的行输入字段值?
- python - concat 的形状错误并在 python 中追加