React条件渲染

在前端的开发当中,我们始终都要进行一些标签显隐,在vue中,由于vue.js的存在,我们可以使用v-if或者v-show对于一个标签进行显隐,而在React中,需要使用js中的判断语句去创建元素表示当前的状态,然后通过React来更新UI。

import React from 'react';
function Hello(props) {
    return <h1>hello world!</h1>;
}

function Hi(props) {
    return <h1>hi world!</h1>;
}
class ConditionRender extends React.Component{
    render(){
        if(false){
            return <Hello></Hello>
        }else{
            return <Hi></Hi>
        }
    }
}
export default ConditionRender;

以上代码在true的时候渲染hello world!在false的时候渲染hi world!;

在vue中:

<template>
  <div>
    <h1 v-if="true">hello world!</h1>
    <h1 v-else>hi world!</h1>
  </div>
</template>

使用三目运算符:

import React from 'react';

class ConditionRender extends React.Component{
    constructor() {
        super();
        this.state={display:false}
    }
    render(){
        return <div>
            <h1>
                {
                    this.state.display?"hello world!":"hi world!"
                }
            </h1>
        </div>
    }
}
export default ConditionRender;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值