加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react综合案例-todolist、localstorage缓存数据

发布时间:2020-12-15 20:34:02 所属栏目:百科 来源:网络整理
导读:1、工具类storage.js var app = { set (key,value){ localStorage.setItem(key,JSON.stringify(value)); }, get (key){ return JSON.parse(localStorage.getItem(key)); },delete(key){ localStorage.removeItem(key); },countFalseNum(key){ let count = ne

1、工具类storage.js

var app ={
    set(key,value){
        localStorage.setItem(key,JSON.stringify(value));
    },get(key){
        return JSON.parse(localStorage.getItem(key));
    },delete(key){
        localStorage.removeItem(key);
    },countFalseNum(key){
        let count=new Number(0);
        let list = JSON.parse(localStorage.getItem(key));
        list.map(function (value,key) {
            if(value.checked==false){
                count = count+1;
            }
        })
        return count;
    },countTrueNum(key){
        let count=new Number(0);
        let list = JSON.parse(localStorage.getItem(key));
        list.map(function (value,key) {
            if(value.checked==false){
                count = count+1;
            }
        })
        return count;
    }
}
export  default app;

2、todolist案例实现

import React from react;
import storage from ../modules/storage;
class Todolist1 extends React.Component{
    constructor(props){
        super(props);
        this.state={
                list:[],finishList:[
                  /*  {
                        title:"录制java",checked:true
                    },{
                        title:"录制react",checked:false
                    },{
                        title:"录制python",checked:true
                    }*/
                ]
        };
    }

    //生命周期函数    页面加载就会触发
    componentDidMount(){
        //获取缓存的数据
        let todoList = storage.get("TodoList");
        if(todoList){
            this.setState({
                finishList:todoList
            })
        }
    }
    addData=(e)=>{
        if(e.keyCode==13){
            let title=this.refs.title.value;
            let tempList = this.state.finishList;
            tempList.push({
                title:title,checked:false
            })
            this.setState({
                list:tempList
            });
            //增加框设置为空
            this.refs.title.value="";
            //缓存数据,使用localStorage,而将一个对象转为字符串使用JSON.stringify()函数
            storage.set("TodoList",tempList);
        }
    }

    changeState=(key)=>{
        let templist =this.state.finishList;
        templist[key].checked =!templist[key].checked;
        this.setState({
            list:templist
        });
        storage.set("TodoList",templist);
    }
    deleteData=(key)=>{
        let templist =this.state.finishList;
        templist.splice(key,1);
        this.setState({
            list:templist
        })
        storage.set("TodoList",templist);
    }

    render(){
        return (
            <div>
                Todolist index
                <h2>Todolist演示</h2>
                <input ref="title" onKeyUp={this.addData}/>
                <hr/>
                <h2>

                </h2>
               <h2>正在进行</h2>

                <hr/>
                {
                    this.state.finishList.map( (value,key)=> {
                        if(value.checked==false){
                            return(
                                <li key={key}>
                                    <input type="checkbox" checked={value.checked} onChange={this.changeState.bind(this,key)}/>{value.title}
                                    ---<button onClick={this.deleteData.bind(this,key)}>删除</button>
                                </li>
                            )
                        }
                    })
                }
                <h2>已完成事项</h2>
                {
                    this.state.finishList.map( (value,key)=> {
                        if(value.checked==true){
                            return(
                                <li key={key}>
                                    <input type="checkbox" checked={value.checked} onChange={this.changeState.bind(this,key)} />{value.title}
                                    ---<button onClick={this.deleteData.bind(this,key)}>删除</button>
                                </li>
                            )
                        }
                    })
                }
                <hr/>



            </div>
        )
    }
}
export  default Todolist1;
View Code

3、app.js加载该组建

import React,{ Component } from react;
import ./assets/css/App.css;
import Todolist1 from ./components/Todolist1;
class App extends Component {
  render() {
    return(
        <div>
            你好
            <Todolist1/>
        </div>
    )
  }
}
export default App;

注意:

1、localStorage的使用
2、this对象的指向

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读