织梦CMS - 轻松建站从此开始!

欧博ABG官网-欧博官方网址-会员登入

Use if staDG游戏tement in React JSX

时间:2026-01-07 14:30来源: 作者:admin 点击: 2 次
Use conditional rendering, and since you have no else-case, you can use && instead of a ternary operator for brevity: It works because in Jav

Use conditional rendering, and since you have no else-case, you can use && instead of a ternary operator for brevity:

It works because in JavaScript, true && expression always evaluates to expression, and false && expression always evaluates to false.

Therefore, if the condition is true, the element right after && will appear in the output. If it is false, React will ignore and skip it.

Thus:

<div className={"chartContent"}> {this.state.modalityGraph['nca'] > 0 && <div className={"chart-container"}> <Chart chartType="ColumnChart" data = { this.state.modalityGraph?this.state.modalityGraph.chartData['units']:emptyDataRows } options={chartOptions} graph_id="modalitiesChart" width="100%" height="250px" /> </div> } </div>

This will only render JSX if a condition is true. If it is false, React won't render anything. Remember, you have to wrap inline JavaScript expressions in JSX with { … }, you can't just have it inside JSX.

Using if/else statements directly in JSX will cause it to be rendered literally as text, which isn't desired. You also can't use them in inline JavaScript expressions because if statements are not expressions, so this won't work:

(责任编辑:)
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2026-01-09 18:01 最后登录:2026-01-09 18:01
栏目列表
推荐内容