React__TodoList 简单增删改查—本地存储-案例

 

  1 import React, { Component } from 'react'
  2 
  3 export default class todolist extends Component {
  4     constructor(props) {
  5         super()
  6         this.state = {
  7             name: []
  9              }
 10         }
 12           //refs
 13     todoonclick = () => {
 14         if (this.refs.inputmy.value === '') {
 15             alert('請輸入')
 16         } else {
 17             let name = this.refs.inputmy.value
 18             this.setState({
 19                 name: [...this.state.name, name]
 20             },()=>{
 21                 window.localStorage.setItem('myList',this.state.name)
 22             })
 23         }
 24         this.refs.inputmy.value = '';
 25           }
 26     render() {
 27         return (
 28             
29 {/* ref */} 30 'text' onKeyDown={(e) => { 31 if (e.keyCode === 13) { 32 this.todoonclick() 33 } 34 }} ref='inputmy'> 35 36
37
    39 { 40 this.state.name.map((item, index) => { 41 return ( 43
  • 'todo_div' key={index}>{item}     45
    46 {/* 修改事件 */} 47 62
    63 64 73
  • ) 74 } 75 ) 76 } 77
78
80 {this.state.name.map((item, index) =>
  • {item} 81
  • )} 82 */} 83
    84 ) } 85 86 //加載时执行 87 componentWillMount() { 88 89 //localStrong 中获取 myList 90 91 var myList = window.localStorage.getItem('myList') 92 if (myList == null || myList === '') { 93 myList = []; 94 } else { 95 myList = myList.split(',') 96 } 97 this.setState({ 98 name: myList 99 }) 100 101 } 102 }

    你可能感兴趣的:(React__TodoList 简单增删改查—本地存储-案例)