javascript - 如何通过 localStorage.getItem() 恢复值
问题描述
这可能是一个新手问题,但我确实设法将值保存在 localStorage 中,我想在重新加载浏览器后恢复。我用布尔值或数字值做到了,但我不知道如何保存/加载数组。我在此过程中使用的代码:
const storedPlayerChips = localStorage.getItem('playerChips');
if (localStorage.getItem('playerChips')) {
this.setState({
playerChips: +localStorage.getItem('playerChips'),
})
} else {
this.setState({
playerChips: 1000,
})
localStorage.setItem('playerChips', JSON.stringify(nextState.playerChips));
解决方案
推荐阅读
- twig - Symfony 树枝 | 生的标签破坏了我的设计
- python - 需要根据每行的字符长度将数据拆分为多列,使用Python
- python - 为什么列表不能与 wx.BoxSizer 一起使用?
- python - 我如何在python中编写aws cli命令
- javascript - Javascript函数乱序整理——异步
- python - 如何绕过网络导致的 SMTP 超时?
- react-native - 使用 react-native 动画 api 与原点进行 react-native-svg 旋转的等价物是什么
- angular - 具有不同路由器插座的多个子组件的问题
- scala - 从宏中的 HList 获取类型序列
- c# - 比较 2 个大字符串列表