React 使用 if else 判断语句
今天在写 React 时,在 render 的return中既然不能使用if判断语句,所以就整理一些在react中使用if 的方式,可根据自己的实际情况选择:
方式一:
class LLL extends React.Component {
    constructor(props){
        super(props);
        this.judge = false
    }
    render(){
        let Message
        if (this.judge) {
            Message = (
                <span>
                   <h5>It`s my life!</h5>
                </span>
            )
        } else {
            Message = (
                <h5>I think that's more than just like it!</h5>
            )
        }
        return(
            <div>
                <h4>Wellcom LLL</h4>
                {Message}
            </div>
        );
    }
}
方式二:
class LLL extends React.Component {
    constructor(props){
        super(props);
        this.judge = false
    }
    Message(){
        if (this.judge) {
            return (
                <span>
                   <h5>It`s my life!</h5>
                </span>
            )
        } else {
            return (
                <h5>I think that's more than just like it!</h5>
            )
        }
    }
    render(){
        return(
            //1
            <div>
                <h4>Wellcom LLL</h4>
                {this.Message()}
            </div>
        );
    }
}
方式三:三元运算符
class LLL extends React.Component {
    constructor(props){
        super(props);
        this.judge = false
    }
    render(){
        return(
            //1
            <div>
                <h4>Wellcom LLL</h4>
                {this.judge ? "It`s my life!" : "I think that's more than just like it!"}
            </div>
        );
    }
}
方式四:
class LLL extends React.Component {
    constructor(props){
        super(props);
        this.judge = false
    }
    render(){
        return(
            //1
            <div>
                <h4>Wellcom LLL</h4>
                {
                    this.judge
                    ?
                    <span>
                        <h5>It`s my life!</h5>
                    </span>
                    :
                    <h5>I think that's more than just like it!</h5>
                }
            </div>
        );
    }
}
React 使用 if else 判断语句的更多相关文章
- SQLite的时候判断语句是否纯在:出现RuntimeException
		
写SQLite的时候判断语句是否纯在: public boolean exist(long id) { String filter = FRIEND_KEY_ID + "=" + ...
 - 第二周:If判断语句程序当中的作用简介
		
1.If语句的作用: 在我们编写程序时经常会遇到内容判断的问题,比如判断内容的真假或者值的大小分别输出内容的问题 这时就会用到我们的If判断语句了,顾名思义,if在英文单词中意思为如果,在Java中他 ...
 - 关于JavaScript的判断语句(1)
		
if语句: if( 判断条件 ){ 判断结果为true执行语句: } if...else语句: if(判断条件){ 判断结果为true时执行的语句: }else{ 判断结果为false时执行语句: } ...
 - SQL判断语句用法和多表查询
		
1.格式化时间sql语句 本例中本人随便做了两张表,和实际不是很相符,只是想说明sql语句的写法. 例1表格式如下: 需求:查询出本表,但需要使time字段的时间格式为yyyy-MM-dd,比如:20 ...
 - VB的判断语句和循环语句
		
判断语句 •If语句 if语句共有4种写法: 第一种语法: If 条件判断语句 then 程序代码 第二种语法:If 条件判断语句 then 程序代码 else 程式代码 第三种语法: If 条件 ...
 - Interview----求 1+2+...+n, 不能用乘除法、for、while if、else、switch、case 等关键字以及条件判断语句 (A?B:C)
		
题目描述: 求 1+2+...+n, 要求不能使用乘除法.for.while.if.else.switch.case 等关键字以及条件判断语句 (A?B:C). 分析: 首先想到的是写递归函数,但是遇 ...
 - 求1+2+…+n,要求不能使用乘除法、for、while、if、else、s witch、case 等关键字以及条件判断语句(A?B:C)和不用循环/goto/递归输出1~100的10种写法
		
来源:据说是某一年某个公司的面试题 题目:求1+2+…+n, 要求不能使用乘除法.for.while.if.else.s witch.case 等关键字以及条件判断语句(A?B:C) 分析:这题本来很 ...
 - aspcms中if判断语句的运用
		
1.<h3 {if:"[list:isrecommend]"="1"} style="color:red;"{end if}>& ...
 - if条件判断语句的不同
		
let number = ["a":1, "b":2, "c":3]; if let num = number["d"] ...
 
随机推荐
- 项目Beta冲刺--1/7
			
项目Beta冲刺--1/7 作业要求 这个作业属于哪个课程 软件工程1916-W(福州大学) 这个作业要求在哪里 项目Beta冲刺 团队名称 基于云的胜利冲锋队 项目名称 云评:高校学生成绩综合评估及 ...
 - loj10017. 「一本通 1.2 练习 4」传送带(三分套三分)
			
题目描述 在一个2维平面上有两条传送带,每一条传送带可以看成是一条线段.两条传送带分别为线段AB和线段CD.lxhgww在AB上的移动速度为P,在CD上的移动速度为Q,在平面上的移动速度R.现在lxh ...
 - Mac下用java代码调用adb命令时出错
			
原本我直接这样写: Process process=Runtime.getRuntime().exec("adb devices"); 但是运行时出错: java.io.IOExc ...
 - Convert.ToByte((int)val)
			
static void Main(string[] args) { ; byte bit8 = Convert.ToByte((int)val); Console.WriteLine("[{ ...
 - Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException 拒绝访问 / 出现了内部错误  c# – 当使用X509Certificate2加载p12/pfx文件时出现
			
环境:iis/netcore 2.2 初始调用:X509Certificate2 certificate = new X509Certificate2(input.Path, CER_PASSWORD ...
 - ent 基本使用十九 事务处理
			
ent 生成的代码中client 提供了比较全的事务处理 启动单个事务进行处理 // GenTx generates group of entities in a transaction. func ...
 - trutle库的使用基础
			
turtle库的使用: 概括: turtle绘图体系:1969年诞生,主要用于程序设计入门 Python语言的标准库之一 入门级的图形绘制函数库 原理: turtle的原(wan)理(fa) (tur ...
 - vlc for mac设置中文的方法
			
VLC for mac是一款mac系统下的多媒体播放器,支持播放MPEG-1.MPEG-2.MPEG-4.DivX.MP3和OGG,以及DVD.VCD.等各种流媒体协议在内的多种协议格式,并且能够对电 ...
 - 使用gdb调试段错误
			
[https://blog.csdn.net/xj9120/article/details/91380074] 1.bt 2.frame number 3.一般是内存问题 4.kill
 - mysql左连接查询结果不准确
			
现有四张表 表(1)res_resource_catalog 表(2)res_catalog_classify 表(3)res_resource_classify 表(4)res_resource_m ...