我正在使用角度来做出反应,我试图找出一个很好的反应,替代角度的ng-if指令,在那里我渲染或不渲染基于条件的元素.以此代码为例.我使用的是typcript(tsx)btw,但这并不重要.
"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;
ternary operator怎么样?
原文链接:https://www.f2er.com/react/301158.htmlrender() { return ( this.props.showMe ? <button type="submit" className="btn nav-btn-red">SIGN UP</button> : null ); }