我正在使用角度来做出反应,我试图找出一个很好的反应,替代角度的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怎么样?
render() { return ( this.props.showMe ? <button type="submit" className="btn nav-btn-red">SIGN UP</button> : null ); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。