加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – 在react.js中相当于ng-if是什么?

发布时间:2020-12-15 20:51:35 所属栏目:百科 来源:网络整理
导读:我正在使用角度来做出反应,我试图找出一个很好的反应,替代角度的ng-if指令,在那里我渲染或不渲染基于条件的元素.以此代码为例.我使用的是typcript(tsx)btw,但这并不重要. "use strict";import * as React from 'react';interface MyProps {showMe: Boolean}i
我正在使用角度来做出反应,我试图找出一个很好的反应,替代角度的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
  );
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读