react如何实现todolist的增删改查

这期内容当中小编将会给大家带来有关react如何实现todolist的增删改查,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

    以todolist为例

    react如何实现todolist的增删改查

    react如何实现todolist的增删改查

    目录如下

    react如何实现todolist的增删改查

    app.js
     import React, {  PureComponent } from 'react'
    import Input from './components/Input'
    import List from './components/List'
    import Total from './components/Total'
    import Mask from './components/Mask'
    import { bus as $bus } from './components/bus'
    import './App.css'
    export default class App extends PureComponent {
      constructor() {
        super()
        this.state = {
          flag: false,
          list: [
            {
              id: 1,
              content: '哈哈哈',
              checked: false
            },
            {
              id: 7,
              content: '哈哈哈',
              checked: false
            },
            {
              id: 5,
              content: '哈哈哈',
              checked: false
            },
          ],
          checkAll: false,
          selectLength: 0,
          item: {}
        }
      }
      // 全选全不选
      checkAllHandler(checked) {
        console.log("checked",checked);
        const { list } = this.state
        const newList = list.map(item =>{
          return {...item,checked}
        })
        this.setState({list:newList,checkAll: checked},()=>{
          this.doneLenth()
        })
      }
      // 单选单不选
      checkHandler =(id,checked)=> {
        const { list } = this.state
        const newList = list.map(item => {
          return item.id === id ? {...item,checked} : item
        })
        let checkAll = newList.length && newList.every(item => item.checked)
        this.setState(() => ({list: newList,checkAll}),()=>{
          this.doneLenth()
        })
      }
      // 添加 
      addHandler = (obj)=>{
        let { list } = this.state;
        let newList = [...list,obj]
        console.log('newList===='+newList)
        this.setState({
          list: newList,
        },()=>{
          this.doneLenth()
        })
      } 
      // 搜索
      searchHandler=(content)=>{
        console.log("content",content);
        let { list } = this.state;
        let newList = list.filter(item => item.content.includes(content))
        this.setState({
          list: newList
        },()=>{
          this.doneLenth()
        })
      }
      // 删除
      delHandler = (id)=> {
        console.log("id",id);
        const { list } = this.state
        const newList = list.filter(item => item.id !=id)
        let checkAll = newList.length && newList.every(item => item.checked)
        this.setState(() => ({list: newList,checkAll}),()=>{
          this.doneLenth()
        })
      }
      // 编辑
      editHandler = (items)=>{
        this.setState({
          item: items
        })
      }
      // 更新
      update = (content)=>{
        const { list,item } = this.state
        let obj = Object.assign(item,{content})
        const newList = list.map(v =>{
          if(v.id === obj.id) {
            v = {...obj}
          }
          return v
        })
        this.setState({
          list: newList,
          item: obj
        })
      }
      // 已完成 
      doneLenth=()=> {
        const { list } = this.state
        const newList = list.filter(item => item.checked)
        let selectLength = newList.length
        setTimeout(()=>{
          this.setState({
            selectLength
          })
        })
      }
      // 挂载
      componentDidMount() {
        this.unSubscribe = $bus.addListener("getFlag",(flag)=>{
          this.setState({flag})
        })
        this.unSubscribe1 = $bus.addListener("sendValue",(obj)=>{
         this.addHandler(obj)
        })
        this.unSubscribe2 = $bus.addListener("searchValue",(value)=>{
         this.searchHandler(value)
        })
        this.unSubscribe3 = $bus.addListener("getItem",(item)=>{
         this.editHandler(item)
        })
        this.unSubscribe4 = $bus.addListener("update",(content)=>{
         this.update(content)
        })
      }
      // 卸载
      componentWillUnmount() {
        $bus.removeListener(this.unSubscribe)
        $bus.removeListener(this.unSubscribe1)
        $bus.removeListener(this.unSubscribe2)
        $bus.removeListener(this.unSubscribe3)
        $bus.removeListener(this.unSubscribe4)
      }
      render() {
        let { flag, list,checkAll,selectLength } = this.state
        return (
          <div className='container'>
            {/* 输入框 */}
            <Input></Input>
            {/* 列表 */}
            <List list={list} checkHandler={this.checkHandler} delHandler={this.delHandler}></List>
            {/* 统计 */}
            <Total checkAllHandler={this.checkAllHandler.bind(this)} checkAll={checkAll} selectLength={selectLength}></Total>
            {/* 编辑弹框 */}
            { flag ? <Mask ></Mask> : ''}
          </div>
        )
      }
    }

    react如何实现todolist的增删改查