今天在写 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 判断语句的更多相关文章

  1. SQLite的时候判断语句是否纯在:出现RuntimeException

    写SQLite的时候判断语句是否纯在: public boolean exist(long id) { String filter = FRIEND_KEY_ID + "=" + ...

  2. 第二周:If判断语句程序当中的作用简介

    1.If语句的作用: 在我们编写程序时经常会遇到内容判断的问题,比如判断内容的真假或者值的大小分别输出内容的问题 这时就会用到我们的If判断语句了,顾名思义,if在英文单词中意思为如果,在Java中他 ...

  3. 关于JavaScript的判断语句(1)

    if语句: if( 判断条件 ){ 判断结果为true执行语句: } if...else语句: if(判断条件){ 判断结果为true时执行的语句: }else{ 判断结果为false时执行语句: } ...

  4. SQL判断语句用法和多表查询

    1.格式化时间sql语句 本例中本人随便做了两张表,和实际不是很相符,只是想说明sql语句的写法. 例1表格式如下: 需求:查询出本表,但需要使time字段的时间格式为yyyy-MM-dd,比如:20 ...

  5. VB的判断语句和循环语句

      判断语句 •If语句 if语句共有4种写法: 第一种语法: If 条件判断语句 then 程序代码 第二种语法:If 条件判断语句 then 程序代码 else 程式代码 第三种语法: If 条件 ...

  6. 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). 分析: 首先想到的是写递归函数,但是遇 ...

  7. 求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) 分析:这题本来很 ...

  8. aspcms中if判断语句的运用

    1.<h3 {if:"[list:isrecommend]"="1"} style="color:red;"{end if}>& ...

  9. if条件判断语句的不同

    let number = ["a":1, "b":2, "c":3]; if let num = number["d"] ...

随机推荐

  1. query_posts函数使用方法小结|wordpress技巧

    query_posts是wordpress非常好用的调用文章函数,可以调用某个分类.标签.日期及作者等不同范围的文章列表.下面随ytkah一起来看看query_posts函数使用方法小结 首先是que ...

  2. crontab每小时运行一次(转)

    https://blog.csdn.net/liu0808/article/details/80668705 先给出crontab的语法格式 对于网上很多给出的每小时定时任务写法,可以说绝大多数都是错 ...

  3. winform窗体的常用属性

  4. 总结TestNg与JUnit的异同

    工作中一直用的是junit,近期稍微学习了一下TestNg,发现TestNg比java强大太多. TestNg简介 TestNg也是一套测试框架,它的灵感来源于Junit(java的单元测试框架)和N ...

  5. Subband Decomposition

    子带分解. 例如语音信号是宽带信号,根据奈奎斯特采样定理,采样率为16kHz的语音信号的有效带宽是8KHz,不论是对于降噪,aec,vad,波束形成亦或是logfbank特征提取,我们都期望更精细的处 ...

  6. elastalert 基于EFK环境的邮件报警

    1.安装elastalert  安装的python必须是2.7以上的版本 安装的路径  /usr/local/src 安装依赖 yum -y install wget openssl openssl- ...

  7. JavaScript base64 加密解密

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. hlt 与 llt 相关

    HLT:HIGH LEVEL TEST,通常指SDV/SIT/SVT等测试活动SDV : system design verification 系统设计验证 SIT : system integrat ...

  9. SpringCloud:搭建基于Gateway的微服务网关(二)

    0.代码 https://github.com/fengdaizang/OpenAPI 1.引入相关依赖 pom文件如下: <?xml version="1.0" encod ...

  10. [WEB安全]源码泄露总结

    原文链接:https://blog.csdn.net/qq_36869808/article/details/88895109 源码泄露总结: svn源码泄露:https://blog.csdn.ne ...