前端基础之《React(12)—条件渲染》

一、条件渲染(类组件写法)

1、在react里做条件渲染,像vue中的v-if、v-else、v-show

2、单一元素的条件渲染

语法:{ bol && <jsx元素 /> }

如果布尔值等于真,后面的jsx元素就渲染,如果布尔值等于假则不渲染

例子:

javascript 复制代码
import { Component } from 'react';

class A extends Component {
    constructor (props) {
        super(props);
        this.state = {
            bol1: true,
        };
    }

    render () {
        const { bol1 } = this.state;

        return (
            <div>
                <h1>类组件</h1>
                <hr />
                { bol1 && <h1>你好</h1> }
                <button onClick={() => this.setState(state => ({ bol1: !state.bol1 }))}>显示/隐藏</button>
            </div>
        );
    }
}

export default A;

state可以用_下划线代替

() => this.setState(_ => ({ bol1: !_.bol1 }))

3、两个元素的条件渲染

语法:{ bol ? <jsx元素1 /> : <jsx元素2 /> }

例子:

javascript 复制代码
import { Component } from 'react';

class A extends Component {
    constructor (props) {
        super(props);
        this.state = {
            bol2: true,
        };
    }

    render () {
        const { bol2 } = this.state;

        return (
            <div>
                <h1>类组件</h1>
                <hr />
                { bol2 ? <h1>北京</h1> : <h1>上海</h1> }
                <button onClick={() => this.setState(state => ({ bol2: !state.bol2 }))}>切换</button>
            </div>
        );
    }
}

export default A;

4、多个元素的条件渲染,建议封装自定义渲染函数

比如:function renderSomething(...arg) { }

传一些自定义参数,在里面可以做任何想做的事情,最后返回一个满足条件的视图结构

例子:

javascript 复制代码
import { Component } from 'react';

class A extends Component {
    constructor (props) {
        super(props);
        this.state = {
            num: 0,
        };
    }

    renderRow () {
        const { num } = this.state;
        // do something
        let result = null;
        if (num === 0) {
            result = <h1>北京</h1>;
        } else if (num === 1) {
            result = <h1>上海</h1>;
        } else if (num === 2) {
            result = <h1>广州</h1>;
        }
        return result;
    }

    render () {
        //const { bol1 } = this.state;

        return (
            <div>
                <h1>类组件</h1>
                <hr />

                { this.renderRow() }
                <button onClick={() => this.setState(state => ({ num: (state.num + 1)%3 }))}>切换</button>

            </div>
        );
    }
}

export default A;

5、实现v-show

使用display:none来实现元素的显示与隐藏

语法:<style={{display:(bol ? '显示' : '隐藏')}} jsx />

style里第一对是jsx的尖括号,第二对是键值对

例子:

javascript 复制代码
import { Component } from 'react';

class A extends Component {
    constructor (props) {
        super(props);
        this.state = {
            bol3: true,
        };
    }

    render () {
        const { bol3 } = this.state;

        return (
            <div>
                <h1>类组件</h1>
                <hr />

                <h1 style={{display:(bol3 ? 'block' : 'none')}}>广州</h1>
                <button onClick={() => this.setState(state => ({ bol3: !state.bol3 }))}>显示/隐藏</button>
            </div>
        );
    }
}

export default A;

二、条件渲染(函数式组件写法)

1、例子

javascript 复制代码
import { useState } from 'react';

function A () {
    const [idx, setIdx] = useState(0);

    // 使用箭头函数封装
    const renderLine = () => {
        let result = null;
        switch(idx) {
            case 0:
                result = <h1>北京</h1>;
                break;
            case 1: 
                result = <h1>上海</h1>;
                break;
            case 2: 
                result = <h1>广州</h1>;
                break;
            default:
                result = null;
                break;
        }
        return result;  
    };

    return (
        <div>
            <h1>函数式组件</h1>
            { renderLine() }
            <button onClick={() => setIdx((idx + 1)%4)}>切换</button>
        </div>
    );
}

export default A;
相关推荐
用户5372312882468 小时前
你的 WGSL 从未被执行过——一个 secure context 静默陷阱的排查实录
前端
Daniel_1238 小时前
轻量级站点监控面板 LightPing 开源啦
前端·后端·github
lerhxx8 小时前
从 Markdown 到生成式 UI:AI 应用中的流式渲染实践
前端·javascript
迅猛龙办公室8 小时前
python实现简单进度条
java·前端·python
Rosanci9 小时前
Codex 下载与本地部署实战:从安装到运行全流程指南
开发语言·前端·算法·chatgpt·codex
zhangzeyuaaa9 小时前
深入 Ruby:Block、Proc、Lambda 核心区别与最佳实践
开发语言·前端·ruby
liangshanbo12159 小时前
前端高级面试题:WebSocket 双向流式通信怎么设计?
前端·websocket·网络协议
可乐鸡翅yeah_9 小时前
新手梳理:HLS 线上问题,哪些该提给 CDN,哪些该找后端切片服务
开发语言·前端·javascript·vue.js·网络协议·http·m3u8在线
zwd20059 小时前
Manim arrange 和 arrange_in_grid 用法详解:buff、aligned_edge、rows/cols(0.21.0 实测)
前端·python·edge
小小龙学IT9 小时前
Go 语言 reflect 反射包深度解析:从 Type/Value 到三大定律与工业实践
前端·golang