1.react 临时缓存数据
checkboxChange=(key)=>{
let temp=this.state.list ;
temp[key].checked=! temp[key].checked
this.setState({
list : temp
})
localStorage.setItem('todoList',JSON.stringify(temp)) //只能保存数据 不能保存对象 临时缓存
}
localStorage.setItem('缓存名',要缓存的数据) // 只能保存数据,不能保存对象,所以使用了JSON.stringify 转换对象为数据
调用生命周期函数:加载时自动触发
localStorage.getItem('缓存名') //获取先解析为JSON对象 JSON.parse
componentDidMount(){
//获取缓存的数据
var list1=JSON.parse(localStorage.getItem('todoList')) ; //获取后解析
if(list1){
this.setState({
list : list1
})
}
}