我开始使用 angular 做出反应,我试图找出一个很好的反应替代 angular 的 ng-if 指令,我根据条件渲染或不渲染元素。以这段代码为例。我正在使用打字稿(tsx)顺便说一句,但这并不重要。
"use strict";
import * as React from 'react';
interface MyProps {showMe: Boolean}
interface MyState {}
class Button extends React.Component <MyProps, MyState>{
constructor(props){
super(props);
this.state = {};
}
render(){
let button;
if (this.props.showMe === true){
button = (
<button type="submit" className="btn nav-btn-red">SIGN UP</button>
)
} else {
button = null;
}
return button;
}
}
export default Button;
此解决方案有效,但是否有另一种通常用于实现此效果的方法?我只是猜测
原文由 ceckenrode 发布,翻译遵循 CC BY-SA 4.0 许可协议
三元运算符 呢?
您也可以使用
&&
:注释中的大块可以通过将 JSX 包装在
()
s 中来轻松处理:也内联: