当前位置: 首页 > news >正文

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"}dangeronClick={() => 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, // 生产不同的idname: 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 = "";}
}
http://www.lryc.cn/news/373913.html

相关文章:

  • 回归预测 | Matlab实现NGO-HKELM北方苍鹰算法优化混合核极限学习机多变量回归预测
  • 操作系统——信号
  • 力扣1482.制作m束花所需的最少时间
  • 解决 Linux 和 Java 1.8 中上传中文名称图片报错问题
  • cocos开发的时候 wx.onShow在vscode里面显示红色
  • 使用 PNPM 从零搭建 Monorepo,测试组件并发布
  • Oracle 19C 数据库表被误删除的模拟恢复
  • 【CICID】GitHub-Actions语法
  • Ionic 创建 APP
  • 【数学代码】幂
  • os.system() 函数
  • Spring Boot中的RESTful API详细介绍及使用
  • nlp学习笔记
  • 使用python获取内存信息
  • 外包公司泛滥,这些常识你应该提前知道?
  • Linux下的抓包工具使用介绍
  • centos环境上:k8s 简单安装教程
  • 短视频矩阵系统/源码搭建---拆解热门视频功能开发上线
  • 手机和模拟器的 Frida 环境配置
  • 力扣1385.两个数组间的距离值
  • [C++] 小游戏 斗破苍穹 2.11.6 版本 zty出品
  • 认识与学习JSP
  • MySql 各种 join
  • 【Android面试八股文】Android中操作多线程的方式有哪些?
  • 语义分割和目标检测的关系
  • SpringBoot 大文件基于md5实现分片上传、断点续传、秒传
  • 数据资产治理与数据质量提升:构建完善的数据治理体系,确保数据资产的高质量与准确性
  • SylixOS下UDP组播测试程序
  • Web前端快速开发平台:革命性工具,提升开发效率的新篇章
  • 内窥镜系统设计简介