React基础教程(07):条件渲染

1 条件渲染

使用条件渲染,结合TodoList案例,进行完善,实现以下功能:

  • 当列表中的数据为空的时候,现实提示信息暂无待办事项
  • 当列表中存在数据的时候,提示信息消失

这里介绍三种实现方式。

注意这里的Empty是前端框架Antd的组件,如果安装了antd直接使用即可,如果没有安装可以用div代替也是可以的。这里为了更直观,所以使用了前端框架antdEmpty组件。

1、三元操作符

{/*方式一*/}
{this.state.list.length === 0 ? <Empty style={{float:"left"}} description={"暂无待办事项"} /> : null}

2、逻辑与操作符

{/*方式二*/}
{ this.state.list.length === 0 && <Empty style={{float:"left"}} description={"暂无待办事项"} />}

3、css控制

方式三,主要是通过给Empty容器绑定className属性为hidden,然后通过className属性,设置容器的display:none,来实现Empty的隐藏与现实,该方法中的Empty是一直都存在的。

{/*方式三*/}
<Empty className={this.state.list.length === 0 ? "" : "hidden"} style={{float:"left"}} description={"暂无待办事项"} />

2、实现效果动图如下

在这里插入图片描述

3、完整代码

import React, {Component} from "react";
import {Button, Empty} from 'antd';
import {DeleteOutlined} from '@ant-design/icons';

import './css/App.css'
import './css/01-index.css'

export default class App extends Component {

    a = 35;

    myRef = React.createRef();

    // 定义状态
    state = {
        list: [
            {
                id: 1,
                name: "凯文·杜兰特"
            },
            {
                id: 2,
                name: "德文·布克"
            },
            {
                id: 3,
                name: "布拉德利·比尔"
            }]
    }

    render() {
        return (
            <div style={{marginTop: 10, marginLeft: 10}}>
                <input style={{width: 200}}
                       ref={this.myRef}/>
                {/*非常推荐*/}
                <Button style={{backgroundColor: '#2ba471', border: "none"}} size={"middle"} type={"primary"}
                        onClick={() => {
                            this.handlerClick() // 非常推荐,传参数
                        }}>添加</Button>

                <ul>
                    {
                        this.state.list.map((item, index) =>
                            <li style={{fontWeight: "bold", fontSize: "20px"}} key={item.id}>{item.name}
                                <Button size={"small"}
                                        style={{marginLeft: 10}}
                                        type={"primary"}
                                        shape={"circle"}
                                        danger
                                        onClick={() => this.handlerDeleteClick(index)}
                                        icon={<DeleteOutlined/>}/>
                            </li>
                        )
                    }
                </ul>

                {/*方式一*/}
                {/*{this.state.list.length === 0 ? <Empty style={{float:"left"}} description={"暂无待办事项"} /> : null}*/}

                {/*方式二*/}
                {/*{ this.state.list.length === 0 && <Empty style={{float:"left"}} description={"暂无待办事项"} />}*/}

                {/*方式三*/}
                <Empty className={this.state.list.length === 0 ? "" : "hidden"} style={{float:"left"}} description={"暂无待办事项"} />
            </div>
        )
    }

    handlerClick = () => {
        console.log("Click4", this.myRef.current.value);

        // 不要这样写,因为不要直接修改状态,可能会造成不可预期的问题
        // this.state.list.push(this.myRef.current.value);

        let newList = [...this.state.list];
        newList.push({
            id: Math.random() * 100000000, // 生产不同的id
            name: this.myRef.current.value
        });

        this.setState({
            list: newList
        })
    }

    handlerDeleteClick(index) {
        console.log("Del-", index);
        // 深复制
        let newList = this.state.list.concat();
        newList.splice(index, 1);
        this.setState({
            list: newList
        })

        // 清空输入框
        this.myRef.current.value = "";
    }
}

相关推荐

  1. REACT 条件渲染

    2024-06-12 23:44:02       16 阅读
  2. 前端学习之——react篇(条件渲染

    2024-06-12 23:44:02       47 阅读
  3. 四、基础篇 vue条件渲染

    2024-06-12 23:44:02       33 阅读
  4. 解锁React条件渲染的全面指南

    2024-06-12 23:44:02       40 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-12 23:44:02       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-12 23:44:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-12 23:44:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-12 23:44:02       20 阅读

热门阅读

  1. 第一章 - 第1节-计算机概述 - 课后习题

    2024-06-12 23:44:02       8 阅读
  2. CSS Display(显示)

    2024-06-12 23:44:02       8 阅读
  3. 新建pdb 打不开 ORA-65104 ORA-25153

    2024-06-12 23:44:02       12 阅读
  4. 哲学家进餐问题

    2024-06-12 23:44:02       4 阅读
  5. ARM 汇编 C语言 for循环

    2024-06-12 23:44:02       7 阅读
  6. day7C++

    2024-06-12 23:44:02       6 阅读
  7. 解封装类的实现【3】

    2024-06-12 23:44:02       10 阅读
  8. <题海拾贝>[递归]2.合并两个有序链表

    2024-06-12 23:44:02       9 阅读
  9. Element ui 快速入门

    2024-06-12 23:44:02       9 阅读
  10. 【x264】lookahead模块的简单分析

    2024-06-12 23:44:02       10 阅读
  11. sam_out 脱发预测

    2024-06-12 23:44:02       5 阅读
  12. web前端分离:解析其深层含义与影响

    2024-06-12 23:44:02       8 阅读