React案例1

一、案例要求:

1.实现组件分类,分为头部输,列表,底部

2.实现输入任务名称渲染到列表里

3.单独删除任务

4.全选与全不选

5.删除勾选任务

二、实现原理

1.完成组件创建

2.通过ref绑定输入框的值,在通过自定义方法参数传回app.js中通过setstate改变列表中渲染数组的值

3. 通过确定被点击的元素数组id值传回app.js再通过filter方法返回id值不为传回id值的新数组,实现删除功能

4.通过循环判断传回值改变state里的值从而改变勾选状态

5.创建一个通过循环判断是否被勾选再通过filter方法传回新数组的方法在把方法放到底部组件进行调用

三、代码:

App.js

/*
 * @Author: wu07 1732042133@qq.com
 * @Date: 2023-02-14 16:31:46
 * @LastEditors: wu07 1732042133@qq.com
 * @LastEditTime: 2023-02-17 09:27:23
 * @FilePath: \react\day05\demo02\tode\src\App.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import React, { Component } from 'react';
import Header from './components/Header';
import List from './components/List';
import Footer from './components/Footer';
import './App.css';
export default class App extends Component {
  state = {
    todos: [
      { id: '001', name: '吃饭', done: true },
      { id: '002', name: '睡觉', done: true },
      { id: '003', name: '打代码', done: false },
      { id: '004', name: '逛街', done: false }
    ]
  };
  addTodo = (e) => {
    const todos = this.state.todos;
    const newTodos = [e, ...todos];
    this.setState({ todos: newTodos });
  };

  change = (e) => {
    const todos = this.state.todos;
    this.setState({ todos: e });
  };
  updateTodo = (id, done) => {
    const { todos } = this.state;
    const newTodos = todos.map((todoObj) => {
      if (todoObj.id === id) return { ...todoObj, done };
      else return todoObj;
    });
    this.setState({ todos: newTodos });
  };
  Delete = (id) => {
    const todos = this.stat
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Refs 是 React 提供的一种访问 DOM 元素或组件实例的方法。通过 Refs,我们可以在组件中直接访问到被 Refs 引用的 DOM 元素或组件实例,从而进行一些特殊的操作。 下面,我来举一个 React Refs 的案例: 假设我们有一个 Input 组件,需要在组件加载完毕后自动获取焦点,我们可以通过 Refs 来实现这个功能。具体的实现步骤如下: 1. 在 Input 组件中定义 Refs: ```jsx class Input extends React.Component { constructor(props) { super(props); this.inputRef = React.createRef(); // 创建 Refs } render() { return <input type="text" ref={this.inputRef} />; } } ``` 2. 在 componentDidMount 生命周期中,使用 Refs 获取到 input 元素,并调用 focus() 方法: ```jsx class Input extends React.Component { constructor(props) { super(props); this.inputRef = React.createRef(); // 创建 Refs } componentDidMount() { this.inputRef.current.focus(); // 获取 input 元素并获取焦点 } render() { return <input type="text" ref={this.inputRef} />; } } ``` 这样,当 Input 组件加载完毕后,它的 input 元素就会自动获取焦点了。 注意,Refs 只能在类组件中使用,不能在函数式组件中使用。另外,Refs 的值可以是回调函数,而不仅仅是 React.createRef() 方法返回的对象。如果使用回调函数的方式,可以在回调函数中访问到组件实例或 DOM 元素,例如: ```jsx class Input extends React.Component { constructor(props) { super(props); this.inputRef = null; // 创建 Refs this.setInputRef = element => { this.inputRef = element; }; } componentDidMount() { this.inputRef.focus(); // 获取 input 元素并获取焦点 } render() { return <input type="text" ref={this.setInputRef} />; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值