react---todoList案例

news/2024/7/15 18:30:43 标签: react.js, 前端, 前端框架

todoList案例效果图

 1.组件拆分

2.操作state数据

state 放在哪个组件:

  • 如果某个组件组件使用: 放在其自身的state中。
  • 如果某些组件使用:放在他们共同的父组件state中(官方称此操作为状态提升)

状态(state)在哪,操作状态的方法就在哪里。

App.jsx维护状态(数据),因此操作状态的新增,删除,选择等方法 要放在App.jsx中

  addTodo = (todoObj) => {
    console.log("App接收的子组件Header传来的参数", todoObj);
    const { todoList } = this.state;
    const newList = [todoObj, ...todoList];
    this.setState({ todoList: newList });
  };
<Header addTodo={this.addTodo}></Header>

3.父子通信

父子通信:父->子通过props传递;子->父通过props传递,要求父提前给子传递一个函数

allCheck = (done) => {
    const { todoList } = this.state;
    console.log("接受子组件footer的值", done);
    const newList = todoList.map((item) => {
      return { ...item, done: done };
    });
    this.setState({ todoList: newList });
  };
  deleteCheck = () => {
    const { todoList } = this.state;
    const newList = todoList.filter((item) => !item.done);
    this.setState({ todoList: newList });
  };

render() {
    const { todoList } = this.state;
    return (
      <div className="container">
         <Footer
          todoList={todoList}
          allCheck={this.allCheck}
          deleteCheck={this.deleteCheck}
        ></Footer>
      </div>
    );
}

父到子传递数据:App向Footer传递列表数据通过props传递,在Footer标签中通过【todoList={todoList}】传递todoList的数据,在Footer中通过【 const {todoList}=this.props】接收数据。

子到父传递数据:以全选为例,Footer要向App传递全选按钮的checked状态值为false还是true,需要事先在App定义好一个方法,并传递给Footer【allCheck={this.allCheck}】,在Footer中通过【 this.props.allCheck(event.target.checked)】传递数据

export default class Footer extends Component {
  handleAllCheck=(event)=>{
    this.props.allCheck(event.target.checked)
  }
  handleDeleteCheck=()=>{
    this.props.deleteCheck()
  }
  render() {
    const {todoList}=this.props
    const count=todoList.length;
    // reduce用来累加
    // const doneCount=todoList.reduce((prev,current)=>{return prev+(current.done?1:0)},0)
    // 精简以上写法
    const doneCount=todoList.reduce((prev,current)=>prev+(current.done?1:0),0)
    return (
      <div className="footer-wrapper">
        <input type="checkbox" checked={count==doneCount&&count!=0?true:false} onChange={this.handleAllCheck} />
        <span>已完成{doneCount}/全部{count}</span>
        <button className="btn btn-danger" onClick={this.handleDeleteCheck}>清除已完成任务</button>
      </div>
    )
  }
}

4.defaultChecked和checked区别

defaultChecked:仅在第一次初次渲染时生效,更新时不受控制;

checked:始终受到控制,必须通过绑定 onChange 事件来控制选中情况。

类似的还有:defaultValue和value

5.关于案例中一些js用法

reduce---用来实现计算已完成的个数

// reduce用来累加
// const doneCount=todoList.reduce((prev,current)=>{return prev+(current.done?1:0)},0)
// 精简以上写法
const doneCount=todoList.reduce((prev,current)=>prev+(current.done?1:0),0)
JavaScript中Reduce() 的6个用例

nanoid---生成唯一的id

import {nanoid} from "nanoid"
const obj={id:nanoid(),name:target.value,done:false}

通过{...item}批量传值

<ul>
  {
    todoList.map(item=>{
       return <Item key={item.id} {...item} updateTodo={updateTodo} deleteTodo= {deleteTodo}/>
     })
 }
 </ul>

传统的 HTML通过【οnclick="demo()"】绑定事件,

在react中【onClick={this.demo}】

或者【onClick={this.demo()}】,加了()必须在demo还有一个回调函数

handleMouse = (flag) => {
    // 注意这里要return一个回调,因为标签中调用时加了()
    return () => {
      this.setState({ mouse: flag });
    };
  };
<button
onClick={this.handleDelete(id)}
className="btn btn-danger"
style={{ display: mouse ? "block" : "none" }}
>
删除
</button>

http://www.niftyadmin.cn/n/426832.html

相关文章

000网络常见的资源推荐

博客 分类: 图解网络 | 小白debug有时骚话连篇&#xff0c;有时硬核图解https://xiaobaidebug.top/categories/%E5%9B%BE%E8%A7%A3%E7%BD%91%E7%BB%9C/网络攻击常见手段总结 | JavaGuide(Java面试 学习指南)本文整理完善自TCP/IP 常见攻击手段 - 暖蓝笔记 - 2021这篇文章。 这…

Qt学习04:QWidget顶层窗口

文章首发于我的个人博客&#xff1a;欢迎大佬们来逛逛 Qt学习04&#xff1a;QWidget顶层窗口 1. QWidget QWidget类是所有可视控件的基类&#xff0c;控件是用户界面的最小元素&#xff0c;用于接受各种事件(如&#xff1a;鼠标、键盘等)并且绘制出来给用户观看。 每个控件…

App移动端测试 —— Monkey的日志

Monkey的日志对于分析脚本的执行情况十分必要。 Monkey 日志由以下几部分组成&#xff1a; 测试命令信息&#xff1a;随机种子 seed、运行次数、可运行应用列表、各事件百分比。” 正文内容从这里开始&#xff08;可直接省略&#xff0c;亦可配图说明&#xff09;。 01—Mon…

Java中BigDecimal的构造注意事项

禁止使用构造方法BigDecimal(double)的方式把 double值转化为BigDecimal对象。 因为:BigDecimal(double)存在精度损失风险&#xff0c;在精确计算或值比较的场景中可能会导致业务逻辑异常。如: BigDecimal g new BigDecimal(0.1F);实际的存储值为:0.10000000149 正例:优先推荐…

JavaScript 数组 函数

目录 1.数组的概念 2.创建数组 2.1 数组创建的方式 2.2利用new 创建数组 2.3 利用数组字面量创建数组 2.4 数据元素的类型 3.获取数组当元素 3.1数组元素的索引 4.遍历数组 4.1数组的长度 5.数组中新增元素 5.1通过修改length 长度新增数组元素 5.2通过修改数组索引…

linux 内核版本和发行版本

当要明确自己的Linux系统的版本号时&#xff0c;大多数情况是用命令确定Linux内核版本的。不过这个还是要与CentOS的版本号&#xff08;就是你使用的Linux系统的发行版本&#xff09;区分开来&#xff0c;这两个不是一个东西。 一、发行版本号 比如当时安装CentOS时&#x…

使用布隆过滤器的flink十亿级数据实时过滤实践一

1项目背景 1.1 需求 实时推荐项目需求如下&#xff1a;根据用户实时行为&#xff08;如关注&#xff0c;播放&#xff0c;收藏)推荐该UP主(关注的up主&#xff0c;播放视频发布up主&#xff0c;收藏up主)或其相似UP主的作品&#xff0c;UP主及相似UP主下的作品是提前离线召回…

apitable-面试体验

背景介绍&#xff0c;vika维格是一家专做互联网产品的企业&#xff0c;apitable是他们的开源在线协作办公工具的企业&#xff0c;该企业全员远程办公&#xff0c;招聘包括React前端、Java后端等职位&#xff0c;于是我想着去面试试试 起因首轮对战奇怪的表单正式面试讲讲js的ev…