React学习之旅----TodoList localstorage本地存储完善版

封装storage。js

var storage = {
  set (key,value) {
    localStorage.setItem(key, JSON.stringify(value))
  },
  get (key) {
    return JSON.parse(localStorage.getItem(key))
  },
  remove (key) {
    return localStorage.removeItem(key)
  }
}
export default storage;

todolist

import React from 'react';
import storage from '../model/storage'
class TodoList2 extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      list: [
        // {
        //   title: '录制ionic',
        //   checked: true
        // },
        // {
        //   title: '录制nodejs',
        //   checked: false
        // }
        // ,
        // {
        //   title: '录制egg.js',
        //   checked: true
        // },
        // {
        //   title: '录制vue',
        //   checked: false
        // }
      ],
      name: 'TodoList2'
    }
  }
  addData = (e) => {
    // console.log(e)
    if (e.keyCode === 13) {
      let title = this.refs.title.value;
      let tempList = this.state.list;

      tempList.push({
        title: title,
        checked: false
      })
      this.setState({
        list: tempList
      })
      //表单置为空
      this.refs.title.value = '';
      // localStorage.setItem('todoList', JSON.stringify(tempList))
      storage.set('todoList',tempList)
    }
  }
  delData = (key) => {
    var temList = this.state.list
    temList.splice(key, 1)
    this.setState({
      list: temList
    })
    storage.set('todoList',temList)
  }
  checkData = (key) => {
    let tempList = this.state.list;
    tempList[key].checked = !tempList[key].checked;
    this.setState({
      list: tempList
    })
    storage.set('todoList',tempList)
  }
  // 生命周期函数 页面加载就会触发
  componentDidMount () {
    var todoList = storage.get('todoList')
    if (todoList) {
      this.setState({
        list: todoList
      })
    }
  }
  render () {
    return (
      <div>
        {this.state.name}
        <br />
        <input ref='title' onKeyUp={this.addData} />
        <h2>待办事项</h2>
        <ul>
          {
            this.state.list.map((value, key) => {
              if (!value.checked) {
                return (
                  <li key={key}>
                    <input type='checkbox' checked={value.checked} onChange={this.checkData.bind(this, key)} />{value.title}-----------<button onClick={this.delData.bind(this, key)}>删除</button></li>
                )
              }
            })
          }
        </ul>
        <h2>已事项</h2>
        <ul>
          {
            this.state.list.map((value, key) => {
              if (value.checked) {
                return (
                  <li key={key}>
                    <input type='checkbox' checked={value.checked} onChange={this.checkData.bind(this, key)} />{value.title}-----------<button onClick={this.delData.bind(this, key)}>删除</button></li>
                )
              }
            })
          }
        </ul>
      </div>
    )
  }
}
export default TodoList2;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑞朋哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值