当前位置: 首页 > 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 = "";}
}

相关文章:

  • iText7画发票PDF——小tips
  • QT:day1
  • Java:111-SpringMVC的底层原理(中篇)
  • 热门开源项目ChatTTS: 国内语音技术突破,实现弯道超车
  • WPF/C#:异常处理
  • 如何使用new和delete操作符进行动态内存分配和释放?
  • 《C语言》文件操作
  • 细说MCU修改回调函数调用模式的方法
  • 爬虫可以不必自己写,使用ChatGPT编写抓取电影评论数据脚本
  • leetcode198 打家劫舍
  • VBA实现关闭Excel自动计算,关闭屏幕刷新
  • 我的创作纪念日(1825天)
  • OrangePi AIpro小试牛刀-目标检测(YoloV5s)
  • docker 容器 network host 模式启动
  • 揭开 Docker 容器的神秘面纱:深入理解容器原理
  • 【每日笔记】【Go学习笔记】2019-01-10 codis proxy处理流程
  • es6要点
  • java第三方包学习之lombok
  • Js基础知识(四) - js运行原理与机制
  • LeetCode刷题——29. Divide Two Integers(Part 1靠自己)
  • SOFAMosn配置模型
  • UEditor初始化失败(实例已存在,但视图未渲染出来,单页化)
  • web标准化(下)
  • 对超线程几个不同角度的解释
  • 对象引论
  • 检测对象或数组
  • raise 与 raise ... from 的区别
  • ​LeetCode解法汇总2583. 二叉树中的第 K 大层和
  • ​无人机石油管道巡检方案新亮点:灵活准确又高效
  • #微信小程序:微信小程序常见的配置传值
  • $(document).ready(function(){}), $().ready(function(){})和$(function(){})三者区别
  • (2)(2.10) LTM telemetry
  • (7)svelte 教程: Props(属性)
  • (android 地图实战开发)3 在地图上显示当前位置和自定义银行位置
  • (搬运以学习)flask 上下文的实现
  • (草履虫都可以看懂的)PyQt子窗口向主窗口传递参数,主窗口接收子窗口信号、参数。
  • (七)MySQL是如何将LRU链表的使用性能优化到极致的?
  • (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境
  • (新)网络工程师考点串讲与真题详解
  • (一)使用Mybatis实现在student数据库中插入一个学生信息
  • (转)关于多人操作数据的处理策略
  • .NET Conf 2023 回顾 – 庆祝社区、创新和 .NET 8 的发布
  • .net core 6 集成 elasticsearch 并 使用分词器
  • .net core webapi 大文件上传到wwwroot文件夹
  • .net framwork4.6操作MySQL报错Character set ‘utf8mb3‘ is not supported 解决方法
  • .net 发送邮件
  • .net6 webapi log4net完整配置使用流程
  • /boot 内存空间不够
  • /etc/apt/sources.list 和 /etc/apt/sources.list.d
  • @Autowired注解的实现原理
  • @PreAuthorize注解
  • @Resource和@Autowired的区别
  • [ 隧道技术 ] cpolar 工具详解之将内网端口映射到公网
  • [22]. 括号生成
  • [ai笔记3] ai春晚观后感-谈谈ai与艺术