react 做todolist , 数据持久化

这是一个使用React编写的Todos管理应用示例。应用包含输入框、列表展示、添加、删除和完成任务的功能,并利用localStorage持久化数据。当页面加载时,会从localStorage中恢复之前的todos状态。
摘要由CSDN通过智能技术生成

import  { React,Component } from 'react'

import './todos.css'

 class Todos extends Component {

        state = {

            inputValue :'', 

            todos:[

             { title: 'learn React' ,  done:false ,id : Date.now() }, 

            { title: 'learn Vue' ,  done:false  , id :Date.now()},

            { title: 'learn JS',  done:false ,  id :Date.now()}

            ]          

        }

     

   // 初始化生命周期函数

   componentWillMount() {

       console.log(this.state.todos , 15);

       let a = JSON.parse(window.localStorage.getItem('todos'))

    if( a ) {

        this.setState({

            todos:a

        })

    }   

  // 强制刷新

           this.forceUpdate()

   }    

// 获取input 中的内容

    handInp = evt => {

        this.setState ({           

            inputValue : evt.target.value.trim() ,

        })   

        // console.log( evt.target.value);

    }

// 提交

    onEnter = evt => {

        // 按回车键提交

        if ( 13 === evt.keyCode &&  evt.target.value.trim() !== '') {

           console.log(this.state.todos , 49);

            let Ao = []

            Ao = this.state.todos

            Ao.push({

                title:evt.target.value.trim() ,

                done:false ,

                id : Date.now()

            })

            this.setState({

                todos:Ao

            })

            console.log('this.state.todos' , this.state.todos);

            this.setState({

                inputValue : ''

            })

           // 存到local storage 中 

            window.localStorage.setItem('todos',JSON.stringify(this.state.todos) )

            this.forceUpdate()

        }

    } 

    // 删除按钮

    delFn = (index ,evt)  => {   

    this.state.todos.splice(index, 1)

         let add = JSON.parse(window.localStorage.getItem('todos'))

         console.log(evt.target.parentNode);

// 找到相同的id 然后把他截取  findindex 返回的是索引位置

        add.splice(add.findIndex( item => item.id == evt.target.parentNode.dataset ) , 1)

     window.localStorage.setItem('todos' , JSON.stringify(this.state.todos))

     this.forceUpdate()

    }

    // 完成按钮

        addFn = index => {

           let finishTodos = this.state.todos        

           finishTodos[index].done = !finishTodos[index].done

               this.setState({

                  todos:finishTodos

               })

         window.localStorage.setItem('todos' , JSON.stringify(this.state.todos))

        

            this.forceUpdate()

        }

        cheekFn = () => {

            alert(" Your are best ===== 宝贝 你是最棒的"

             )

        }

    render() {

        return (

            <div>

                要完成的目标  :  <input type="text"  onChange={ this.handInp }

                onKeyUp={this.onEnter }  value={this.state.inputValue} />

   

        <ul>

            <h3>   要做的目标 </h3>

            {

                 (this.state.todos? this.state.todos:[]).map((item , index) => {

                 if (!item.done)                  

                        return ( 

                        <li key= {index} data-id={item.id}>

                        <button onClick={ () => this.addFn(index)  }>   finish 🉑 </button>

                        {item.title}   

                        <button onClick={  (evt) => this.delFn (index ,evt) }> delete ❌ </button>    

                        </li>

                        )  

                        }

              

                ) 

                              

                

            }

                        

                </ul>

                <hr />

                <ul className="finish">

                    <h3>  已完成的事情 </h3>

                    {   

                                    

                   (this.state.todos? this.state.todos:[]).map( (item , index )=> {

                        if (item.done) {

                     return ( 

                        <li key={index} className="bb">

                        <button onClick= {  (evt) => this.delFn (index , evt)  }> end 👌 </button>

                                <h3> 完成的目标 </h3>

                        

                               {item.title}   

   

                           <button onClick={this.cheekFn} className="big"> That's great 🦾</button>

                           

                           </li> )

                        }                                             

                    }

                    )

                    }

                </ul>

            </div>

        );

    }

}

export default Todos;




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值